Failed Conditions
Pull Request — master (#30)
by Maximo
04:14 queued 12s
created

Resources::getByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Models;
6
7
use Phalcon\Di;
8
use Gewaer\Exception\ModelException;
9
10
class Resources extends AbstractModel
11
{
12
    /**
13
     *
14
     * @var integer
15
     */
16
    public $id;
17
18
    /**
19
     *
20
     * @var string
21
     */
22
    public $name;
23
24
    /**
25
     *
26
     * @var string
27
     */
28
    public $description;
29
30
    /**
31
     *
32
     * @var integer
33
     */
34
    public $apps_id;
35
36
    /**
37
     *
38
     * @var string
39
     */
40
    public $created_at;
41
42
    /**
43
     *
44
     * @var string
45
     */
46
    public $updated_at;
47
48
    /**
49
     *
50
     * @var integer
51
     */
52
    public $is_deleted;
53
54
    /**
55
     * Initialize method for model.
56
     */
57 7
    public function initialize()
58
    {
59 7
        $this->setSource('resources');
60
61 7
        $this->hasMany(
62 7
            'id',
63 7
            'Gewaer\Models\ResourcesAccesses',
64 7
            'resources_id',
65
            [
66 7
                'alias' => 'accesses',
67
                'params' => [
68 7
                    'conditions' => 'apps_id = ' . $this->di->getApp()->getId()
0 ignored issues
show
Bug Best Practice introduced by
The property di does not exist on Gewaer\Models\Resources. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getApp() does not exist on Phalcon\Mvc\Model\Resultset. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
                    'conditions' => 'apps_id = ' . $this->di->/** @scrutinizer ignore-call */ getApp()->getId()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
                ]
70
            ]
71
        );
72 7
    }
73
74
    /**
75
     * is this name a resource?
76
     *
77
     * @param string $resourceName
78
     * @return boolean
79
     */
80 1
    public static function isResource(string $resourceName) : bool
81
    {
82 1
        return (bool) self::count([
83 1
            'conditions' => 'name = ?0 AND apps_id in (?1, ?2)',
84 1
            'bind' => [$resourceName, Di::getDefault()->getApp()->getId(), Apps::GEWAER_DEFAULT_APP_ID]
85
        ]);
86
    }
87
88
    /**
89
     * Get a resource by it name
90
     *
91
     * @param  string  $resourceName
92
     * @return ResourcesDB
0 ignored issues
show
Bug introduced by
The type Gewaer\Models\ResourcesDB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
     */
94 5
    public static function getByName(string $resourceName) : Resources
95
    {
96 5
        $resource = self::findFirst([
97 5
            'conditions' => 'name = ?0 AND apps_id in (?1, ?2)',
98 5
            'bind' => [$resourceName, Di::getDefault()->getApp()->getId(), Apps::GEWAER_DEFAULT_APP_ID]
99
        ]);
100
101 5
        if (!is_object($resource)) {
102
            throw new ModelException(_('Resource ' . $resourceName . ' not found on this app ' . Di::getDefault()->getApp()->getId()));
103
        }
104
105 5
        return $resource;
106
    }
107
108
    /**
109
     * Returns table name mapped in the model.
110
     *
111
     * @return string
112
     */
113 7
    public function getSource(): string
114
    {
115 7
        return 'resources';
116
    }
117
}
118