Completed
Pull Request — master (#64)
by Thibaud
03:06
created

Monitor::getEntity()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 21
ccs 0
cts 14
cp 0
rs 9.0534
cc 4
eloc 12
nc 1
nop 2
crap 20
1
<?php
2
3
/*
4
 * This file is part of Phraseanet SDK.
5
 *
6
 * (c) Alchemy <[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 PhraseanetSDK;
13
14
use PhraseanetSDK\Http\APIGuzzleAdapter;
15
use Doctrine\Common\Collections\ArrayCollection;
16
17
/**
18
 * @method Monitor getScheduler()
19
 * @method Monitor startTask($task_id)
20
 * @method Monitor stopTask($task_id)
21
 * @method Monitor getTask($task_id)
22
 * @method Monitor getTasks()
23
 */
24
class Monitor
25
{
26
    /**
27
     * @var APIGuzzleAdapter
28
     */
29
    private $adapter;
30
31
    private static $mappings = array(
32
        'getScheduler' => array(
33
            'path'            => 'v1/monitor/scheduler/',
34
            'entity'          => 'PhraseanetSDK\Monitor\Scheduler',
35
            'query-keys'      => array(),
36
            'result-property' => 'scheduler',
37
            'method'          => 'GET',
38
        ),
39
        'getTask'      => array(
40
            'path'            => 'v1/monitor/task/{task_id}/',
41
            'entity'          => 'PhraseanetSDK\Monitor\Task',
42
            'query-keys'      => array('task_id'),
43
            'result-property' => 'task',
44
            'method'          => 'GET',
45
        ),
46
        'startTask'      => array(
47
            'path'            => 'v1/monitor/task/{task_id}/start/',
48
            'entity'          => 'PhraseanetSDK\Monitor\Task',
49
            'query-keys'      => array('task_id'),
50
            'result-property' => 'task',
51
            'method'          => 'POST',
52
        ),
53
        'stopTask'      => array(
54
            'path'            => 'v1/monitor/task/{task_id}/stop/',
55
            'entity'          => 'PhraseanetSDK\Monitor\Task',
56
            'query-keys'      => array('task_id'),
57
            'result-property' => 'task',
58
            'method'          => 'POST',
59
        ),
60
        'getTasks'     => array(
61
            'path'            => 'v1/monitor/tasks/',
62
            'entity'          => 'PhraseanetSDK\Monitor\Task',
63
            'query-keys'      => array(),
64
            'result-property' => 'tasks',
65
            'method'          => 'GET',
66
        ),
67
    );
68
69 6
    public function __construct(APIGuzzleAdapter $adapter)
70
    {
71 6
        $this->adapter = $adapter;
72 6
    }
73
74 5
    public function __call($name, $arguments)
75
    {
76 5
        if (!isset(static::$mappings[$name])) {
0 ignored issues
show
Bug introduced by
Since $mappings is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mappings to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
77
            throw new \BadMethodCallException(sprintf('Method "%s::%s" does not exist.', get_class($this), $name));
78
        }
79
80 5
        return $this->doCall($name, $arguments);
81
    }
82
83 5
    private function doCall($name, $arguments)
84
    {
85 5
        $parameters = array();
86
87 5
        $n = 0;
88 5
        foreach (static::$mappings[$name]['query-keys'] as $key) {
0 ignored issues
show
Bug introduced by
Since $mappings is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mappings to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
89 3
            $parameters[$key] = $arguments[$n];
90 3
            $n++;
91 5
        }
92
93 5
        $response = $this->adapter->call(
94 5
            static::$mappings[$name]['method'],
0 ignored issues
show
Bug introduced by
Since $mappings is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mappings to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
95 5
            array(static::$mappings[$name]['path'], $parameters)
0 ignored issues
show
Bug introduced by
Since $mappings is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mappings to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Documentation introduced by
array(static::$mappings[...]['path'], $parameters) is of type array<integer,?,{"0":"?","1":"array"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96 5
        );
97
        $result = $response->getResult()->{static::$mappings[$name]['result-property']};
0 ignored issues
show
Bug introduced by
Since $mappings is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mappings to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
98
99
        if (is_array($result)) {
100
            $output = new ArrayCollection();
101
            foreach ($result as $entityData) {
102
                $output->add($entity = $this->getEntity($name, $entityData));
103
            }
104
        } else {
105
            $output = $this->getEntity($name, $result);
106
        }
107
108
        return $output;
109
    }
110
111
    private function getEntity($name, $data)
112
    {
113
        $entity = new static::$mappings[$name]['entity']();
0 ignored issues
show
Bug introduced by
Since $mappings is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mappings to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
114
115
        array_walk($data, function ($value, $property) use ($entity) {
116
            $method = 'set'.implode('', array_map(function ($chunk) {
117
                    return ucfirst($chunk);
118
            }, preg_split('/[-_]/', $property)));
119
120
            $ref = new \ReflectionParameter(array($entity, $method), 0);
121
            if (null !== $ref->getClass()) {
122
                if ('DateTime' === $ref->getClass()->name) {
123
                    $value = \DateTime::createFromFormat(DATE_ATOM, $value) ?: null;
124
                }
125
            }
126
127
            call_user_func(array($entity, $method), $value);
128
        });
129
130
        return $entity;
131
    }
132
}
133