JwtController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 120
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A options() 0 8 1
A actionCreate() 0 26 4
A actionUpdate() 0 26 3
A actionDelete() 0 19 3
1
<?php
2
/**
3
 * JwtController.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author Philippe Gaultier <[email protected]>
8
 * @copyright 2010-2017 Philippe Gaultier
9
 * @license http://www.sweelix.net/license license
10
 * @version 1.2.0
11
 * @link http://www.sweelix.net
12
 * @package sweelix\oauth2\server\commands
13
 */
14
15
namespace sweelix\oauth2\server\commands;
16
17
use yii\console\Controller;
18
use Yii;
19
use yii\console\ExitCode;
20
21
/**
22
 * Manage oauth jwts
23
 *
24
 * @author Philippe Gaultier <[email protected]>
25
 * @copyright 2010-2017 Philippe Gaultier
26
 * @license http://www.sweelix.net/license license
27
 * @version 1.2.0
28
 * @link http://www.sweelix.net
29
 * @package sweelix\oauth2\server\commands
30
 * @since 1.0.0
31
 */
32
class JwtController extends Controller
33
{
34
    public $clientId;
35
    public $subject;
36
37
    /**
38
     * @var string alias to public key file
39
     */
40
    public $publicKey;
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function options($actionID)
46
    {
47
        return [
48
            'clientId',
49
            'subject',
50
            'publicKey',
51
        ];
52
    }
53
54
    /**
55
     * Create new Oauth Jwt
56
     * @param string $clientId Should be clientId
57
     * @param string $subject
58
     * @param string $publicKey
59
     * @return int
60
     * @throws \yii\base\InvalidConfigException
61
     * @throws \yii\base\UnknownClassException
62
     * @since 1.0.0
63
     */
64
    public function actionCreate($clientId, $subject, $publicKey)
65
    {
66
        $jwt = Yii::createObject('sweelix\oauth2\server\interfaces\JwtModelInterface');
67
        /* @var \sweelix\oauth2\server\interfaces\JwtModelInterface $jwt */
68
        $jwt->clientId = $clientId;
0 ignored issues
show
Bug introduced by
Accessing clientId on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69
        $jwt->subject = $subject;
0 ignored issues
show
Bug introduced by
Accessing subject on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
70
        if ($this->publicKey !== null) {
71
            $this->publicKey = Yii::getAlias($this->publicKey);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->publicKey) can also be of type boolean. However, the property $publicKey is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
72
            if (file_exists($this->publicKey) === true) {
73
                $jwt->publicKey = file_get_contents($this->publicKey);
0 ignored issues
show
Bug introduced by
Accessing publicKey on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
74
            }
75
        } else {
76
            $jwt->publicKey = $publicKey;
0 ignored issues
show
Bug introduced by
Accessing publicKey on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
77
        }
78
        if ($jwt->save() === true) {
79
            $this->stdout('Jwt created :' . "\n");
80
            $this->stdout(' - id: ' . $jwt->id . "\n");
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
81
            $this->stdout(' - clientId: ' . $jwt->clientId . "\n");
0 ignored issues
show
Bug introduced by
Accessing clientId on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
82
            $this->stdout(' - subject: ' . $jwt->subject . "\n");
0 ignored issues
show
Bug introduced by
Accessing subject on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
83
            $this->stdout(' - publicKey: ' . $jwt->publicKey . "\n");
0 ignored issues
show
Bug introduced by
Accessing publicKey on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
84
            return ExitCode::OK;
85
        } else {
86
            $this->stdout('Jwt cannot be created.' . "\n");
87
            return ExitCode::UNSPECIFIED_ERROR;
88
        }
89
    }
90
91
    /**
92
     * Update Oauth jwt
93
     * @param string $id Jwt id
94
     * @return int
95
     * @throws \yii\base\InvalidConfigException
96
     * @throws \yii\base\UnknownClassException
97
     */
98
    public function actionUpdate($id)
99
    {
100
        $jwt = Yii::createObject('sweelix\oauth2\server\interfaces\JwtModelInterface');
101
        $jwtClass = get_class($jwt);
102
        /* @var \sweelix\oauth2\server\interfaces\JwtModelInterface $jwt */
103
        $jwt = $jwtClass::findOne($id);
104
        if ($jwt !== null) {
105
            $jwt->clientId = $this->clientId;
0 ignored issues
show
Bug introduced by
Accessing clientId on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
106
            $jwt->subject = $this->subject;
0 ignored issues
show
Bug introduced by
Accessing subject on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
107
            $jwt->publicKey = $this->publicKey;
0 ignored issues
show
Bug introduced by
Accessing publicKey on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
108
            if ($jwt->save() === true) {
109
                $this->stdout('Jwt updated :' . "\n");
110
                $this->stdout(' - id: ' . $jwt->id . "\n");
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
111
                $this->stdout(' - clientId: ' . $jwt->clientId . "\n");
0 ignored issues
show
Bug introduced by
Accessing clientId on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
112
                $this->stdout(' - subject: ' . $jwt->subject . "\n");
0 ignored issues
show
Bug introduced by
Accessing subject on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
113
                $this->stdout(' - publicKey: ' . $jwt->publicKey . "\n");
0 ignored issues
show
Bug introduced by
Accessing publicKey on the interface sweelix\oauth2\server\interfaces\JwtModelInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
114
                return ExitCode::OK;
115
            } else {
116
                $this->stdout('Jwt ' . $id . ' cannot be updated' . "\n");
117
                return ExitCode::UNSPECIFIED_ERROR;
118
            }
119
        } else {
120
            $this->stdout('Jwt ' . $id . ' does not exist' . "\n");
121
            return ExitCode::UNSPECIFIED_ERROR;
122
        }
123
    }
124
125
    /**
126
     * Delete Oauth jwt
127
     * @param string $id Jwt id
128
     * @return int
129
     * @throws \yii\base\InvalidConfigException
130
     * @throws \yii\base\UnknownClassException
131
     */
132
    public function actionDelete($id)
133
    {
134
        $jwt = Yii::createObject('sweelix\oauth2\server\interfaces\JwtModelInterface');
135
        $jwtClass = get_class($jwt);
136
        /* @var \sweelix\oauth2\server\interfaces\JwtModelInterface $jwt */
137
        $jwt = $jwtClass::findOne($id);
138
        if ($jwt !== null) {
139
            if ($jwt->delete() === true) {
140
                $this->stdout('Jwt ' . $id . ' deleted' . "\n");
141
                return ExitCode::OK;
142
            } else {
143
                $this->stdout('Jwt ' . $id . ' cannot be deleted' . "\n");
144
                return ExitCode::UNSPECIFIED_ERROR;
145
            }
146
        } else {
147
            $this->stdout('Jwt ' . $id . ' does not exist' . "\n");
148
            return ExitCode::UNSPECIFIED_ERROR;
149
        }
150
    }
151
}
152