Skip to content

Commit ee73f1d

Browse files
authored
Merge pull request #13 from othercodes/master
Implemented PSR-1 and PSR-2
2 parents cbb4d3c + 7fbb646 commit ee73f1d

13 files changed

+297
-291
lines changed

examples/config.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"apiKey": "XXX:YYY",
3-
"apiEndpoint": "https://host/public/v1",
4-
"logLevel": "info",
5-
"timeout": 10,
6-
"sslVerifyHost": true
2+
"apiKey": "XXX:YYY",
3+
"apiEndpoint": "https://host/public/v1",
4+
"logLevel": "info",
5+
"timeout": 10,
6+
"sslVerifyHost": true
77
}

examples/example.php

Lines changed: 54 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,82 @@
22

33
require "../vendor/autoload.php";
44

5-
// logging library
6-
// configuration through ENV references
7-
85

96
class MyAppRequests extends \Connect\RequestsProcessor
107
{
11-
function __construct()
12-
{
13-
parent::__construct('config.json');
14-
}
15-
16-
function processRequest($req)
17-
{
18-
// print_r($req);
19-
$req->asset->tiers['customer']->name;
20-
$req->asset->tiers['tier1']->name;
21-
22-
$req->status;
23-
$req->id;
24-
$req->type;
25-
26-
$p1 = $req->asset->params['param_a']->error('xxxx');
27-
// $p2 = $req->asset->params['param_b']->error('yyyy')->value('true');
28-
$p2 = new \Connect\Param('param_b', 'true');
29-
$req->requestProcessor->updateParameters($req, [$p1, $p2]);
30-
31-
foreach($req->asset->items as $item)
32-
{
33-
// ...
34-
}
35-
36-
foreach($req->getNewItems() as $item)
37-
{
38-
// provision new Items
39-
$item->mpn;
40-
\Connect\Logger::get()->info($item->id." New ".($item->quantity + 5));
41-
$item->old_quantity;
42-
}
43-
44-
foreach($req->getChangedItems() as $item)
45-
{
46-
// update items
47-
$item->mpn;
48-
\Connect\Logger::get()->info($item->id." Change: ".($item->quantity + 5)." -> ".($item->old_quantity));
49-
}
50-
51-
foreach($req->getRemovedItems() as $item)
52-
{
53-
// unprovision items
54-
\Connect\Logger::get()->info($item->id." Change: ".($item->old_quantity + 5)." -> ".($item->quantity));
55-
}
56-
57-
$req->asset->params['param_a'];
58-
8+
public function __construct()
9+
{
10+
parent::__construct('config.json');
11+
}
12+
13+
public function processRequest($req)
14+
{
15+
16+
$req->asset->tiers['customer']->name;
17+
$req->asset->tiers['tier1']->name;
18+
19+
$req->status;
20+
$req->id;
21+
$req->type;
22+
23+
$p1 = $req->asset->params['param_a']->error('xxxx');
24+
25+
$p2 = new \Connect\Param('param_b', 'true');
26+
$req->requestProcessor->updateParameters($req, [$p1, $p2]);
27+
28+
foreach ($req->asset->items as $item) {
29+
// ...
30+
}
31+
32+
foreach ($req->getNewItems() as $item) {
33+
// provision new Items
34+
$item->mpn;
35+
\Connect\Logger::get()->info($item->id . " New " . ($item->quantity + 5));
36+
$item->old_quantity;
37+
}
38+
39+
foreach ($req->getChangedItems() as $item) {
40+
// update items
41+
$item->mpn;
42+
\Connect\Logger::get()->info($item->id . " Change: " . ($item->quantity + 5) . " -> " . ($item->old_quantity));
43+
}
44+
45+
foreach ($req->getRemovedItems() as $item) {
46+
// unprovision items
47+
\Connect\Logger::get()->info($item->id . " Change: " . ($item->old_quantity + 5) . " -> " . ($item->quantity));
48+
}
49+
50+
$req->asset->params['param_a'];
51+
5952
// if (!isset($req->asset->params['param_a']))
6053
// throw new \Connect\Inquire(array( 'param_a' => 'ActivationID should be set' ));
6154
// throw new \Connect\Inquire(array(
6255
// $req->asset->params['param_a']->error('xxxx'),
6356
// $req->asset->params['param_b']->error('yyyy')->value('default')
6457
// ));
65-
58+
6659
// if (!isset($req->asset->params['param_b']))
6760
// throw new \Connect\Fail(array( 'param_b' => 'Your activation code is already used' ));
6861

69-
throw new \Connect\Skip();
62+
throw new \Connect\Skip();
7063

71-
return "activation succeeded";
72-
}
64+
return "activation succeeded";
65+
}
7366
}
7467

7568

7669
try {
77-
$rp = new MyAppRequests();
7870

71+
$rp = new MyAppRequests();
7972
$rp->process();
8073

81-
// throw new Exception('Some unexpected error happened');
74+
// throw new Exception('Some unexpected error happened');
8275

8376
} catch (Exception $e) {
84-
\Connect\Logger::get()->error($e->getMessage());
85-
86-
// Dump to log all level records
87-
\Connect\Logger::get()->dump();
77+
78+
\Connect\Logger::get()->error($e->getMessage());
79+
// Dump to log all level records
80+
\Connect\Logger::get()->dump();
8881
}
8982

9083
// $rlist = $rp->listRequests(array('status' => 'inquiring'));

src/ActivationTileResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ActivationTileResponse
2626
*/
2727
public function __construct($msg = null)
2828
{
29-
if($msg){
29+
if ($msg) {
3030
$this->activationTile = $msg;
3131
}
3232
}

src/Config.php

Lines changed: 96 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,43 @@
22

33
/**
44
* This file is part of the Ingram Micro Cloud Blue Connect SDK.
5-
*
6-
* @copyright (c) 2018. Ingram Micro. All Rights Reserved.
7-
*/
5+
*
6+
* @copyright (c) 2018. Ingram Micro. All Rights Reserved.
7+
*/
88

99
namespace Connect;
1010

11-
class Config
11+
class Config
1212
{
13-
/**
14-
* @var - string Connect QuickStart API Key
15-
*/
16-
public $apiKey;
17-
18-
/**
19-
* @var string - Connect QuickStart API Endpoint URI
20-
*/
21-
public $apiEndpoint;
22-
23-
/**
24-
* @var array of strings - list of products to work with
25-
*/
26-
public $products;
27-
28-
/**
29-
* @var int - logLevel - what messages to write to log
30-
*/
31-
public $logLevel = LoggerInterface::LEVEL_INFO;
32-
33-
/**
34-
* @var int - network interaction timeout, seconds
35-
*/
36-
public $timeout = 50;
37-
38-
/**
39-
* @var bool - do we need to verify SSL certificate of server
40-
*/
41-
public $sslVerifyHost = true;
13+
/**
14+
* @var - string Connect QuickStart API Key
15+
*/
16+
public $apiKey;
17+
18+
/**
19+
* @var string - Connect QuickStart API Endpoint URI
20+
*/
21+
public $apiEndpoint;
22+
23+
/**
24+
* @var array of strings - list of products to work with
25+
*/
26+
public $products;
27+
28+
/**
29+
* @var int - logLevel - what messages to write to log
30+
*/
31+
public $logLevel = LoggerInterface::LEVEL_INFO;
32+
33+
/**
34+
* @var int - network interaction timeout, seconds
35+
*/
36+
public $timeout = 50;
37+
38+
/**
39+
* @var bool - do we need to verify SSL certificate of server
40+
*/
41+
public $sslVerifyHost = true;
4242

4343
/**
4444
* @param mixed $config -
@@ -48,67 +48,73 @@ class Config
4848
* @throws ConfigPropertyInvalid
4949
* @throws \ReflectionException
5050
*/
51-
public function __construct($config)
52-
{
53-
if (is_string($config)) {
54-
try {
55-
$txt = file_get_contents($config);
56-
} catch (\Exception $e) {
57-
throw new ConfigException("Can't read file $config: " . $e->getMessage());
58-
}
59-
60-
try {
61-
$config = json_decode($txt, true);
62-
} catch (\Exception $e) {
63-
throw new ConfigException("Can't parse JSON in file $config: " . $e->getMessage());
64-
}
65-
}
66-
67-
if (!is_array($config))
68-
throw new ConfigException("Invalid argument for \\Connect\\Config class constructor: " . gettype($config));
69-
70-
$ref = new \ReflectionClass($this);
71-
foreach ($ref->getProperties() as $prop) {
72-
$name = $prop->getName();
73-
74-
if (!isset($config[$name]))
75-
continue;
76-
77-
$value = $config[$name];
78-
79-
if ($name == 'products') {
80-
$prop->setValue($this, is_array($value) ? $value : array($value));
81-
} elseif ($name == 'logLevel') {
82-
$found = false;
83-
foreach (LoggerInterface::LEVELS as $k => $v) {
84-
if (strtoupper($value) == $v) {
85-
$prop->setValue($this, $k);
86-
$found = true;
87-
}
88-
}
89-
if (!$found)
90-
throw new ConfigPropertyInvalid('Unknown log level', $name, $value);
91-
} elseif ($name == "sslVerifyHost") {
92-
if (!is_bool($value))
93-
throw new ConfigPropertyInvalid('Should be boolean', $name, $value);
94-
$prop->setValue($this, $value);
95-
} else {
96-
$prop->setValue($this,$value);
97-
}
98-
}
99-
}
51+
public function __construct($config)
52+
{
53+
if (is_string($config)) {
54+
try {
55+
$txt = file_get_contents($config);
56+
} catch (\Exception $e) {
57+
throw new ConfigException("Can't read file $config: " . $e->getMessage());
58+
}
59+
60+
try {
61+
$config = json_decode($txt, true);
62+
} catch (\Exception $e) {
63+
throw new ConfigException("Can't parse JSON in file $config: " . $e->getMessage());
64+
}
65+
}
66+
67+
if (!is_array($config)) {
68+
throw new ConfigException("Invalid argument for \\Connect\\Config class constructor: " . gettype($config));
69+
}
70+
71+
$ref = new \ReflectionClass($this);
72+
foreach ($ref->getProperties() as $prop) {
73+
$name = $prop->getName();
74+
75+
if (!isset($config[$name])) {
76+
continue;
77+
}
78+
79+
$value = $config[$name];
80+
81+
if ($name == 'products') {
82+
$prop->setValue($this, is_array($value) ? $value : array($value));
83+
} elseif ($name == 'logLevel') {
84+
$found = false;
85+
foreach (LoggerInterface::LEVELS as $k => $v) {
86+
if (strtoupper($value) == $v) {
87+
$prop->setValue($this, $k);
88+
$found = true;
89+
}
90+
}
91+
if (!$found) {
92+
throw new ConfigPropertyInvalid('Unknown log level', $name, $value);
93+
}
94+
} elseif ($name == "sslVerifyHost") {
95+
if (!is_bool($value)) {
96+
throw new ConfigPropertyInvalid('Should be boolean', $name, $value);
97+
}
98+
$prop->setValue($this, $value);
99+
} else {
100+
$prop->setValue($this, $value);
101+
}
102+
}
103+
}
100104

101105
/**
102106
* Validate configuration
103107
* @throws ConfigPropertyMissed
104108
*/
105109
public function validate()
106-
{
107-
if (!isset($this->apiKey))
108-
throw new ConfigPropertyMissed('apiKey');
109-
110-
if (!isset($this->apiEndpoint))
111-
throw new ConfigPropertyMissed('apiEndpoint');
112-
113-
}
110+
{
111+
if (!isset($this->apiKey)) {
112+
throw new ConfigPropertyMissed('apiKey');
113+
}
114+
115+
if (!isset($this->apiEndpoint)) {
116+
throw new ConfigPropertyMissed('apiEndpoint');
117+
}
118+
119+
}
114120
}

src/ConfigException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public function __construct($message, $prop = null)
2323
parent::__construct($message, 'config');
2424
$this->property = $prop;
2525

26-
if ($prop)
26+
if ($prop) {
2727
$this->message = $message . " for property " . $prop;
28+
}
2829
}
2930
}

src/ConfigPropertyInvalid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Configuration property invalid exception
1414
* @package Connect
1515
*/
16-
class ConfigPropertyInvalid extends ConfigException
16+
class ConfigPropertyInvalid extends ConfigException
1717
{
1818
public function __construct($message, $prop, $value)
1919
{

0 commit comments

Comments
 (0)