EntityService::checkRepository()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Service;
12
13
use InvalidArgumentException;
14
use Longman\Platfourm\Contracts\Repository\Repository;
15
16
abstract class EntityService
17
{
18
19
    protected function checkRepository()
20
    {
21
        if (!($this->repository instanceof Repository)) {
0 ignored issues
show
Bug introduced by
The property repository 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...
22
            throw new RepositoryNotFoundException;
23
        }
24
    }
25
26
    protected function parseResult($result)
27
    {
28
        return $result;
29
    }
30
31
    /**
32
     * @param  array $input
33
     * @throws InvalidValueException
34
     * @return mixed
35
     */
36
    public function dispatch(array $input, $methodName = 'run')
37
    {
38
        $method = new ReflectionMethod($this, $methodName);
39
        $params = $method->getParameters();
40
        //var_dump($params);
41
        $args = [];
42
        if (!empty($params)) {
43
            foreach ($params as $item) {
44
                if (!isset($input[$item->getName()])) {
45
                    if ($item->isDefaultValueAvailable()) {
46
                        $input[$item->getName()] = $item->getDefaultValue();
47
                    } else {
48
                        throw new InvalidArgumentException("field not found " . $item->getName());
49
                    }
50
                }
51
52
                $args[] = $input[$item->getName()];
53
            }
54
        }
55
56
        return call_user_func_array([$this, $methodName], $args);
57
    }
58
}
59