Resources::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
ccs 0
cts 11
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Models;
6
7
use Baka\Database\Exception\ModelNotFoundException;
0 ignored issues
show
Bug introduced by
The type Baka\Database\Exception\ModelNotFoundException 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...
8
use Phalcon\Di;
9
use Canvas\Exception\ModelException;
10
11
/**
12
 * Class Resources.
13
 *
14
 * @package Canvas\Models
15
 *
16
 * @property \Phalcon\Di $di
17
 */
18
class Resources extends AbstractModel
19
{
20
    /**
21
     *
22
     * @var integer
23
     */
24
    public $id;
25
26
    /**
27
     *
28
     * @var string
29
     */
30
    public $name;
31
32
    /**
33
     *
34
     * @var string
35
     */
36
    public $description;
37
38
    /**
39
     *
40
     * @var integer
41
     */
42
    public $apps_id;
43
44
    /**
45
     *
46
     * @var string
47
     */
48
    public $created_at;
49
50
    /**
51
     *
52
     * @var string
53
     */
54
    public $updated_at;
55
56
    /**
57
     *
58
     * @var integer
59
     */
60
    public $is_deleted;
61
62
    /**
63
     * Initialize method for model.
64
     */
65
    public function initialize()
66
    {
67
        $this->setSource('resources');
68
69
        $this->hasMany(
70
            'id',
71
            'Canvas\Models\ResourcesAccesses',
72
            'resources_id',
73
            [
74
                'alias' => 'accesses',
75
                'params' => [
76
                    'conditions' => 'apps_id = ' . $this->di->getApp()->getId()
77
                ]
78
            ]
79
        );
80
    }
81
82
    /**
83
     * is this name a resource?
84
     *
85
     * @param string $resourceName
86
     * @return boolean
87
     */
88
    public static function isResource(string $resourceName) : bool
89
    {
90
        return (bool) self::count([
91
            'conditions' => 'name = ?0 AND apps_id in (?1, ?2)',
92
            'bind' => [
93
                $resourceName,
94
                Di::getDefault()->getApp()->getId(),
95
                Apps::CANVAS_DEFAULT_APP_ID
96
            ]
97
        ]);
98
    }
99
100
    /**
101
     * Get a resource by it name.
102
     *
103
     * @param  string  $resourceName
104
     * @return Resources
105
     */
106
    public static function getByName(string $resourceName) : Resources
107
    {
108
        $resource = self::findFirst([
109
            'conditions' => 'name = ?0 AND apps_id in (?1, ?2)',
110
            'bind' => [
111
                $resourceName,
112
                Di::getDefault()->getApp()->getId(),
113
                Apps::CANVAS_DEFAULT_APP_ID
114
            ]
115
        ]);
116
117
        if (!is_object($resource)) {
118
            throw new ModelNotFoundException(
119
                _('Resource ' . $resourceName . ' not found on this app ' . Di::getDefault()->getApp()->getId())
120
            );
121
        }
122
123
        return $resource;
124
    }
125
126
    /**
127
     * Returns table name mapped in the model.
128
     *
129
     * @return string
130
     */
131
    public function getSource(): string
132
    {
133
        return 'resources';
134
    }
135
}
136