KeyController::actionCreate()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 22
cp 0
rs 8.5386
c 0
b 0
f 0
cc 7
nc 12
nop 1
crap 56
1
<?php
2
/**
3
 * KeyController.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 keys
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 KeyController extends Controller
33
{
34
35
    /**
36
     * @var string alias to public key file
37
     */
38
    public $publicKey;
39
    /**
40
     * @var string alias to private key file
41
     */
42
    public $privateKey;
43
    /**
44
     * @var string encryption algorithm
45
     */
46
    public $encryptionAlgorithm;
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function options($actionID)
52
    {
53
        return [
54
            'publicKey',
55
            'privateKey',
56
            'encryptionAlgorithm',
57
        ];
58
    }
59
60
    /**
61
     * Create new Oauth CypherKey
62
     * @param string $id Should be client-id or default for common key
63
     * @return int
64
     * @throws \yii\base\InvalidConfigException
65
     * @throws \yii\base\UnknownClassException
66
     * @since 1.0.0
67
     */
68
    public function actionCreate($id)
69
    {
70
        $cypherKey = Yii::createObject('sweelix\oauth2\server\interfaces\CypherKeyModelInterface');
71
        /* @var \sweelix\oauth2\server\interfaces\CypherKeyModelInterface $cypherKey */
72
        $cypherKey->id = $id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\in...CypherKeyModelInterface 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...
73
        $hasKey = false;
74
        if (($this->privateKey !== null) && ($this->publicKey !== null)) {
75
            $this->privateKey = Yii::getAlias($this->privateKey);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->privateKey) can also be of type boolean. However, the property $privateKey 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...
76
            $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...
77
            if ((file_exists($this->privateKey) === true) && (file_exists($this->publicKey) === true)) {
78
                $cypherKey->privateKey = file_get_contents($this->privateKey);
0 ignored issues
show
Bug introduced by
Accessing privateKey on the interface sweelix\oauth2\server\in...CypherKeyModelInterface 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...
79
                $cypherKey->publicKey = file_get_contents($this->publicKey);
0 ignored issues
show
Bug introduced by
Accessing publicKey on the interface sweelix\oauth2\server\in...CypherKeyModelInterface 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...
80
                $hasKey = true;
81
            }
82
        }
83
        if ($hasKey === false) {
84
            $cypherKey->generateKeys();
85
        }
86
        if ($cypherKey->save() === true) {
87
            $this->stdout('Key created :' . "\n");
88
            $this->stdout(' - id: ' . $cypherKey->id . "\n");
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\in...CypherKeyModelInterface 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...
89
            $this->stdout(' - Algorithm: ' . $cypherKey->encryptionAlgorithm . "\n");
0 ignored issues
show
Bug introduced by
Accessing encryptionAlgorithm on the interface sweelix\oauth2\server\in...CypherKeyModelInterface 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...
90
            return ExitCode::OK;
91
        } else {
92
            $this->stdout('Key cannot be created.' . "\n");
93
            return ExitCode::UNSPECIFIED_ERROR;
94
        }
95
    }
96
97
    /**
98
     * Update existing Oauth CypherKey
99
     * @param $id
100
     * @return int
101
     * @throws \yii\base\InvalidConfigException
102
     * @throws \yii\base\UnknownClassException
103
     */
104
    public function actionUpdate($id)
105
    {
106
        $cypherKey = Yii::createObject('sweelix\oauth2\server\interfaces\CypherKeyModelInterface');
107
        $cypherKeyClass = get_class($cypherKey);
108
        /* @var \sweelix\oauth2\server\interfaces\CypherKeyModelInterface $cypherKey */
109
        $cypherKey = $cypherKeyClass::findOne($id);
110
        if ($cypherKey !== null) {
111
            $hasKey = false;
112
            if (($this->privateKey !== null) && ($this->publicKey !== null)) {
113
                $this->privateKey = Yii::getAlias($this->privateKey);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($this->privateKey) can also be of type boolean. However, the property $privateKey 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...
114
                $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...
115
                if ((file_exists($this->privateKey) === true) && (file_exists($this->publicKey) === true)) {
116
                    $cypherKey->privateKey = file_get_contents($this->privateKey);
0 ignored issues
show
Bug introduced by
Accessing privateKey on the interface sweelix\oauth2\server\in...CypherKeyModelInterface 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...
117
                    $cypherKey->publicKey = file_get_contents($this->publicKey);
0 ignored issues
show
Bug introduced by
Accessing publicKey on the interface sweelix\oauth2\server\in...CypherKeyModelInterface 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...
118
                    $hasKey = true;
119
                }
120
            }
121
            if ($hasKey === false) {
122
                $cypherKey->generateKeys();
123
            }
124
            if ($cypherKey->save() === true) {
125
                $this->stdout('Key updated :' . "\n");
126
                $this->stdout(' - id: ' . $cypherKey->id . "\n");
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\in...CypherKeyModelInterface 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...
127
                $this->stdout(' - Algorithm: ' . $cypherKey->encryptionAlgorithm . "\n");
0 ignored issues
show
Bug introduced by
Accessing encryptionAlgorithm on the interface sweelix\oauth2\server\in...CypherKeyModelInterface 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...
128
                return ExitCode::OK;
129
            } else {
130
                $this->stdout('Key ' . $id . ' cannot be updated' . "\n");
131
                return ExitCode::UNSPECIFIED_ERROR;
132
            }
133
        } else {
134
            $this->stdout('Key ' . $id . ' does not exist' . "\n");
135
            return ExitCode::UNSPECIFIED_ERROR;
136
        }
137
    }
138
139
    /**
140
     * Delete Oauth CypherKey
141
     * @param $id
142
     * @return int
143
     * @throws \yii\base\InvalidConfigException
144
     * @throws \yii\base\UnknownClassException
145
     */
146
    public function actionDelete($id)
147
    {
148
        $cypherKey = Yii::createObject('sweelix\oauth2\server\interfaces\CypherKeyModelInterface');
149
        $cypherKeyClass = get_class($cypherKey);
150
        /* @var \sweelix\oauth2\server\interfaces\CypherKeyModelInterface $cypherKey */
151
        $cypherKey = $cypherKeyClass::findOne($id);
152
        if ($cypherKey !== null) {
153
            if ($cypherKey->delete() === true) {
154
                $this->stdout('Key ' . $id . ' deleted' . "\n");
155
                return ExitCode::OK;
156
            } else {
157
                $this->stdout('Key ' . $id . ' cannot be deleted' . "\n");
158
                return ExitCode::UNSPECIFIED_ERROR;
159
            }
160
        } else {
161
            $this->stdout('Key ' . $id . ' does not exist' . "\n");
162
            return ExitCode::UNSPECIFIED_ERROR;
163
        }
164
    }
165
}
166