Completed
Push — master ( 6ba8a0...7b1561 )
by Nick
07:06
created

Inspector::findEndpoint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace EloquentJs\ScriptGenerator\Model;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Inspector
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $excludeScopes = ['scopeScope', 'scopeEloquentJs'];
14
15
    /**
16
     * @var Repository
17
     */
18
    protected $config;
19
20
    /**
21
     * Create a new Inspector instance.
22
     *
23
     * @param Repository $config
24
     */
25
    public function __construct(Repository $config)
26
    {
27
        $this->config = $config;
28
    }
29
30
    /**
31
     * Inspect a model class and return its metadata.
32
     *
33
     * @param Model $instance
34
     * @return Metadata
35
     */
36
    public function inspect(Model $instance)
37
    {
38
        return new Metadata(
39
            class_basename($instance),
40
            $this->findEndpoint($instance),
41
            $this->findDateColumns($instance),
42
            $this->findScopeMethods($instance)
43
        );
44
    }
45
46
    /**
47
     * Find the endpoint for this model.
48
     *
49
     * @param Model $instance
50
     * @return string|null
51
     */
52
    protected function findEndpoint(Model $instance)
53
    {
54
        if ($instance->getEndpoint()) {
55
            return $instance->getEndpoint();
56
        }
57
58
        return $this->readModelConfig($instance, 'endpoint');
59
    }
60
61
    /**
62
     * Get any additional date columns for this model.
63
     *
64
     * @param Model $instance
65
     * @return array
66
     */
67
    protected function findDateColumns(Model $instance)
68
    {
69
        return array_values(
70
            array_diff($instance->getDates(), ['created_at', 'updated_at', 'deleted_at'])
71
        );
72
    }
73
74
    /**
75
     * Get the scope methods for this model with 'scope' prefix removed.
76
     *
77
     * @param Model $instance
78
     * @return array
79
     */
80
    protected function findScopeMethods(Model $instance)
81
    {
82
        return array_map(
83
            function($method) {
84
                return lcfirst(substr($method, 5));
85
            },
86
            array_values(
87
                array_filter(
88
                    get_class_methods($instance),
89
                    function($method) {
90
                        return substr($method, 0, 5) === 'scope' and ! in_array($method, $this->excludeScopes);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
91
                    }
92
                )
93
            )
94
        );
95
    }
96
97
    /**
98
     * Read from a model config value from the eloquentjs.php config file.
99
     *
100
     * @param Model $instance
101
     * @param string $key
102
     * @return mixed
103
     */
104
    protected function readModelConfig(Model $instance, $key)
105
    {
106
        $className = get_class($instance);
107
108
        return $this->config->get("eloquentjs.generator.{$className}.{$key}");
109
    }
110
}
111