Test Failed
Pull Request — master (#9)
by Maximo
03:13
created

Roles::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\Models;
5
6
use Gewaer\Exception\ServerErrorHttpException;
7
use Baka\Auth\Models\Companies;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Gewaer\Models\Companies. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
class Roles extends AbstractModel
10
{
11
    /**
12
     *
13
     * @var integer
14
     */
15
    public $id;
16
17
    /**
18
     *
19
     * @var integer
20
     */
21
    public $name;
22
23
    /**
24
     *
25
     * @var integer
26
     */
27
    public $description;
28
29
    /**
30
     *
31
     * @var integer
32
     */
33
    public $scope;
34
35
    /**
36
     *
37
     * @var integer
38
     */
39
    public $company_id;
40
41
    /**
42
     *
43
     * @var int
44
     */
45
    public $apps_id;
46
47
    /**
48
     *
49
     * @var string
50
     */
51
    public $created_at;
52
53
    /**
54
     *
55
     * @var string
56
     */
57
    public $updated_at;
58
59
    /**
60
     *
61
     * @var integer
62
     */
63
    public $is_deleted;
64
65
    /**
66
     * Default ACL company
67
     *
68
     */
69
    const DEFAULT_ACL_COMPANY_ID = 0;
70
    const DEFAULT_ACL_APP_ID = 0;
71
72
    /**
73
     * Initialize method for model.
74
     */
75 4
    public function initialize()
76
    {
77 4
        $this->setSource('roles');
78 4
    }
79
80
    /**
81
     * Returns table name mapped in the model.
82
     *
83
     * @return string
84
     */
85 4
    public function getSource(): string
86
    {
87 4
        return 'roles';
88
    }
89
90
    /**
91
     * Get the Role by it app name
92
     *
93
     * @param string $role
94
     * @return Roles
95
     */
96 2
    public function getByAppName(string $role, Companies $company): Roles
97
    {
98
        //echeck if we have a dot , taht means we are sending the specific app to use
99 2
        if (strpos($role, '.') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($role, '.') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
100
            throw new ServerErrorHttpException('ACL - We are expecting the app for this role');
101
        }
102
103 2
        $appRole = explode('.', $role);
104 2
        $role = $appRole[1];
1 ignored issue
show
Unused Code introduced by
The assignment to $role is dead and can be removed.
Loading history...
105 2
        $appName = $appRole[0];
106
107
        //look for the app and set it
108 2
        if (!$app = Apps::getACLApp($appName)) {
109
            throw new ServerErrorHttpException('ACL - No app found for this role');
110
        }
111
112 2
        return self::findFirst([
1 ignored issue
show
Bug Best Practice introduced by
The expression return self::findFirst(a...FAULT_ACL_COMPANY_ID))) returns the type Phalcon\Mvc\Model which includes types incompatible with the type-hinted return Gewaer\Models\Roles.
Loading history...
113 2
            'conditions' => 'apps_id in (?0, ?1) AND company_id in (?2 , ?3)',
114 2
            'bind' => [$app->getId(), self::DEFAULT_ACL_APP_ID, $company->getId(), self::DEFAULT_ACL_COMPANY_ID]
115
        ]);
116
    }
117
}
118