Completed
Push — master ( e81e8a...e81e8a )
by Claude
01:07
created

Emarsys::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0052

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 11
cts 12
cp 0.9167
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
crap 3.0052
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)
0 ignored issues
show
Unused Code introduced by
The parameter $secret is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48 6
        $this->parameters = array(
0 ignored issues
show
Bug introduced by
The property parameters does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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