Completed
Push — master ( 7b1561...963a80 )
by Nick
05:50
created

Inspector::readModelConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
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
            $this->findRelations($instance)
44
        );
45
    }
46
47
    /**
48
     * Find the endpoint for this model.
49
     *
50
     * @param Model $instance
51
     * @return string|null
52
     */
53
    protected function findEndpoint(Model $instance)
54
    {
55
        if ($instance->getEndpoint()) {
56
            return $instance->getEndpoint();
57
        }
58
59
        return $this->readModelConfig($instance, 'endpoint');
60
    }
61
62
    /**
63
     * Get any additional date columns for this model.
64
     *
65
     * @param Model $instance
66
     * @return array
67
     */
68
    protected function findDateColumns(Model $instance)
69
    {
70
        return array_values(
71
            array_diff($instance->getDates(), ['created_at', 'updated_at', 'deleted_at'])
72
        );
73
    }
74
75
    /**
76
     * Get the scope methods for this model with 'scope' prefix removed.
77
     *
78
     * @param Model $instance
79
     * @return array
80
     */
81
    protected function findScopeMethods(Model $instance)
82
    {
83
        return array_map(
84
            function($method) {
85
                return lcfirst(substr($method, 5));
86
            },
87
            array_values(
88
                array_filter(
89
                    get_class_methods($instance),
90
                    function($method) {
91
                        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...
92
                    }
93
                )
94
            )
95
        );
96
    }
97
98
    /**
99
     * Get a map of relation method names and their related models.
100
     *
101
     * For example, if the given model has a "comments" method that
102
     * describes the relation to a Comment model, this returns
103
     * ['comments' => 'Comment']
104
     *
105
     * @param  Model  $instance
106
     * @return array
107
     */
108
    protected function findRelations(Model $instance)
109
    {
110
        $relations = $this->readModelConfig($instance, 'relations', []);
111
112
        return array_map(function ($relation) { return class_basename($relation); }, $relations);
113
    }
114
115
    /**
116
     * Read from a model config value from the eloquentjs.php config file.
117
     *
118
     * @param Model $instance
119
     * @param string $key
120
     * @param mixed $default
121
     * @return mixed
122
     */
123
    protected function readModelConfig(Model $instance, $key, $default = null)
124
    {
125
        $className = get_class($instance);
126
127
        return $this->config->get("eloquentjs.generator.{$className}.{$key}", $default);
128
    }
129
}
130