1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* A php library for using the Emarsys API. |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/quitoque/emarsys-php-client |
7
|
|
|
* @package emarsys-php-client |
8
|
|
|
* @license MIT |
9
|
|
|
* @copyright Copyright (c) 2017 Quitoque <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Emarsys; |
13
|
|
|
|
14
|
|
|
use Emarsys\Api\ApiInterface; |
15
|
|
|
use Emarsys\Exception\Api\LogicException; |
16
|
|
|
use Emarsys\Exception\Api\NotFoundException; |
17
|
|
|
use Emarsys\HttpClient\Builder; |
18
|
|
|
use Http\Client\Common\PluginClient; |
19
|
|
|
use Http\Message\MessageFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Emarsys. |
23
|
|
|
* |
24
|
|
|
* @author Claude Khedhiri <[email protected]> |
25
|
|
|
* |
26
|
|
|
* @method Api\Contacts contacts() |
27
|
|
|
* @method Api\Fields fields() |
28
|
|
|
*/ |
29
|
|
|
class Emarsys |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var PluginClient |
33
|
|
|
*/ |
34
|
|
|
private $httpClient; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Builder |
38
|
|
|
*/ |
39
|
|
|
private $httpBuilder; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var MessageFactory |
43
|
|
|
*/ |
44
|
|
|
private $requestFactory; |
45
|
|
|
|
46
|
6 |
|
public function __construct($username, $secret) |
|
|
|
|
47
|
|
|
{ |
48
|
6 |
|
$this->parameters = array( |
|
|
|
|
49
|
6 |
|
'username' => $username, |
50
|
6 |
|
'secret' => $username, |
51
|
|
|
); |
52
|
6 |
|
} |
53
|
|
|
|
54
|
3 |
|
public function __call($name, $arguments) |
55
|
|
|
{ |
56
|
3 |
|
$class = sprintf('\Emarsys\Api\%s', ucfirst($name)); |
57
|
|
|
|
58
|
3 |
|
if (false === class_exists($class)) { |
59
|
1 |
|
throw new NotFoundException($class); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
if ($class instanceof ApiInterface) { |
63
|
|
|
throw new LogicException(sprintf('%s must be extend Emarsys\Api\ApiInterface')); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$this->initialize(); |
67
|
|
|
|
68
|
2 |
|
$class = new $class( |
69
|
2 |
|
$this->httpClient, |
70
|
2 |
|
$this->requestFactory |
71
|
2 |
|
); |
72
|
|
|
|
73
|
2 |
|
return $class; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $httpBuilder |
78
|
|
|
*/ |
79
|
2 |
|
public function setHttpBuilder($httpBuilder) |
80
|
|
|
{ |
81
|
2 |
|
$this->httpBuilder = $httpBuilder; |
82
|
2 |
|
} |
83
|
|
|
|
84
|
2 |
|
private function initialize() |
85
|
|
|
{ |
86
|
2 |
|
if (null !== $this->httpClient && null !== $this->requestFactory) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
if (null === $this->httpBuilder) { |
91
|
2 |
|
$this->httpBuilder = new Builder(); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
2 |
|
$this->httpBuilder->setParameters($this->parameters); |
95
|
|
|
|
96
|
2 |
|
$this->httpClient = $this->httpBuilder->getHttpClient(); |
97
|
2 |
|
$this->requestFactory = $this->httpBuilder->getRequestFactory(); |
98
|
2 |
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.