Failed Conditions
Pull Request — master (#30)
by Maximo
03:25
created

Resources::isResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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