Client   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 63
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setToken() 0 4 1
A call() 0 19 4
A getConnection() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\PiwikBundle\Client;
13
14
use Core23\PiwikBundle\Connection\ConnectionInterface;
15
use Core23\PiwikBundle\Exception\PiwikException;
16
17
final class Client implements ClientInterface
18
{
19
    /**
20
     * @var ConnectionInterface
21
     */
22
    private $connection;
23
24
    /**
25
     * @var string
26
     */
27
    private $token;
28
29
    /**
30
     * Initialize Piwik client.
31
     *
32
     * @param ConnectionInterface $connection Piwik active connection
33
     * @param string              $token      auth token
34
     */
35
    public function __construct(ConnectionInterface $connection, string $token = 'anonymous')
36
    {
37
        $this->connection = $connection;
38
        $this->setToken($token);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function setToken(string $token): void
45
    {
46
        $this->token = $token;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function call(string $method, array $params = [], $format = 'php')
53
    {
54
        $params['method']     = $method;
55
        $params['token_auth'] = $this->token;
56
        $params['format']     = $format;
57
        $data                 = $this->getConnection()->send($params);
58
59
        if ('php' === $format) {
60
            $object = unserialize($data);
61
62
            if (isset($object['result']) && 'error' === $object['result']) {
63
                throw new PiwikException($object['message']);
64
            }
65
66
            return $object;
67
        }
68
69
        return $data;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getConnection(): ConnectionInterface
76
    {
77
        return $this->connection;
78
    }
79
}
80