ClientController::actionUpdate()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 8
cp 0
rs 9.1448
c 0
b 0
f 0
cc 5
nc 9
nop 1
crap 30
1
<?php
2
/**
3
 * ClientController.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 sweelix\oauth2\server\models\Client;
18
use yii\console\Controller;
19
use Yii;
20
use yii\console\ExitCode;
21
22
/**
23
 * Manage oauth clients
24
 *
25
 * @author Philippe Gaultier <[email protected]>
26
 * @copyright 2010-2017 Philippe Gaultier
27
 * @license http://www.sweelix.net/license license
28
 * @version 1.2.0
29
 * @link http://www.sweelix.net
30
 * @package sweelix\oauth2\server\commands
31
 * @since 1.0.0
32
 */
33
class ClientController extends Controller
34
{
35
36
    public $redirectUri;
37
    public $grantTypes;
38
    public $scopes;
39
    public $userId;
40
    public $name;
41
    public $isPublic;
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function options($actionID)
47
    {
48
        return [
49
            // Generated 'id',
50
            // Generated 'secret',
51
            'redirectUri',
52
            'grantTypes',
53
            'scopes',
54
            'userId',
55
            'name',
56
            'isPublic'
57
        ];
58
    }
59
60
    /**
61
     * Create new Oauth client
62
     * @return int
63
     * @throws \yii\base\InvalidConfigException
64
     * @throws \yii\base\UnknownClassException
65
     * @since 1.0.0
66
     */
67
    public function actionCreate()
68
    {
69
70
        $client = Yii::createObject('sweelix\oauth2\server\interfaces\ClientModelInterface');
71
        /* @var \sweelix\oauth2\server\interfaces\ClientModelInterface $client */
72
        $client->id = $this->getRandomString();
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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
        $client->secret = $this->getRandomString();
0 ignored issues
show
Bug introduced by
Accessing secret on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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
        $client->name = $this->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
75
        $redirectUri = empty($this->redirectUri) ? null : explode(',', $this->redirectUri);
76
        $client->redirectUri = $redirectUri;
0 ignored issues
show
Bug introduced by
Accessing redirectUri on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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
        $client->userId = $this->userId;
0 ignored issues
show
Bug introduced by
Accessing userId on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
78
        $client->isPublic = (bool)$this->isPublic;
0 ignored issues
show
Bug introduced by
Accessing isPublic on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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
        $client->scopes = empty($this->scopes) ? null : explode(',', $this->scopes);
0 ignored issues
show
Bug introduced by
Accessing scopes on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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
        $client->grantTypes = empty($this->grantTypes) ? null : explode(',', $this->grantTypes);
0 ignored issues
show
Bug introduced by
Accessing grantTypes on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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
        if ($client->save() === true) {
82
            $this->stdout('Client created :' . "\n");
83
            $this->stdout(' - id: ' . $client->id . "\n");
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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
            $this->stdout(' - secret: ' . $client->secret . "\n");
0 ignored issues
show
Bug introduced by
Accessing secret on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
85
            $this->stdout(' - name: ' . $client->name . "\n");
0 ignored issues
show
Bug introduced by
Accessing name on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
86
            $this->stdout(' - redirectUri: ' . implode(',', $client->redirectUri) . "\n");
0 ignored issues
show
Bug introduced by
Accessing redirectUri on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
87
            return ExitCode::OK;
88
        } else {
89
            $this->stdout('Client cannot be created.' . "\n");
90
            return ExitCode::UNSPECIFIED_ERROR;
91
        }
92
    }
93
94
    /**
95
     * Generate random string
96
     * @param int $length
97
     * @return string
98
     * @since 1.0.0
99
     */
100
    protected function getRandomString($length = 40)
101
    {
102
        $bytes = (int)$length / 2;
103
        return bin2hex(openssl_random_pseudo_bytes($bytes));
104
    }
105
106
    /**
107
     * Update Oauth client
108
     * @param $id
109
     * @return int
110
     * @throws \yii\base\UnknownClassException
111
     * @throws \yii\base\InvalidConfigException
112
     */
113
    public function actionUpdate($id)
114
    {
115
        $client = Yii::createObject('sweelix\oauth2\server\interfaces\ClientModelInterface');
116
        $clientClass = get_class($client);
117
        /* @var \sweelix\oauth2\server\interfaces\ClientModelInterface $client */
118
        $client = $clientClass::findOne($id);
119
        if ($client !== null) {
120
            $client->redirectUri = $this->redirectUri;
0 ignored issues
show
Bug introduced by
Accessing redirectUri on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
121
            $client->name = $this->name;
0 ignored issues
show
Bug introduced by
Accessing name on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
122
            $client->userId = $this->userId;
0 ignored issues
show
Bug introduced by
Accessing userId on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
123
            $client->isPublic = (bool)$this->isPublic;
0 ignored issues
show
Bug introduced by
Accessing isPublic on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
124
            $client->scopes = empty($this->scopes) ? null : explode(',', $this->scopes);
0 ignored issues
show
Bug introduced by
Accessing scopes on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
125
            $client->grantTypes = empty($this->grantTypes) ? null : explode(',', $this->grantTypes);
0 ignored issues
show
Bug introduced by
Accessing grantTypes on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
126
            if ($client->save() === true) {
127
                $this->stdout('Client updated :' . "\n");
128
                $this->stdout(' - id: ' . $client->id . "\n");
0 ignored issues
show
Bug introduced by
Accessing id on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
129
                $this->stdout(' - secret: ' . $client->secret . "\n");
0 ignored issues
show
Bug introduced by
Accessing secret on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
130
                $this->stdout(' - name: ' . $client->name . "\n");
0 ignored issues
show
Bug introduced by
Accessing name on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
131
                $this->stdout(' - redirectUri: ' . implode(',', $client->redirectUri) . "\n");
0 ignored issues
show
Bug introduced by
Accessing redirectUri on the interface sweelix\oauth2\server\in...es\ClientModelInterface 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...
132
                return ExitCode::OK;
133
            } else {
134
                $this->stdout('Client ' . $id . ' cannot be updated.' . "\n");
135
                return ExitCode::UNSPECIFIED_ERROR;
136
            }
137
        } else {
138
            $this->stdout('Client ' . $id . ' does not exist' . "\n");
139
            return ExitCode::UNSPECIFIED_ERROR;
140
        }
141
    }
142
143
    /**
144
     * Delete Oauth client
145
     * @param $id
146
     * @return int
147
     * @throws \yii\base\InvalidConfigException
148
     * @throws \yii\base\UnknownClassException
149
     */
150
    public function actionDelete($id)
151
    {
152
        $client = Yii::createObject('sweelix\oauth2\server\interfaces\ClientModelInterface');
153
        $clientClass = get_class($client);
154
        /* @var \sweelix\oauth2\server\interfaces\ClientModelInterface $client */
155
        $client = $clientClass::findOne($id);
156
        if ($client !== null) {
157
            if ($client->delete() === true) {
158
                $this->stdout('Client ' . $id . ' deleted' . "\n");
159
                return ExitCode::OK;
160
            } else {
161
                $this->stdout('Client ' . $id . ' cannot be deleted.' . "\n");
162
                return ExitCode::UNSPECIFIED_ERROR;
163
            }
164
        } else {
165
            $this->stdout('Client ' . $id . ' does not exist' . "\n");
166
            return ExitCode::UNSPECIFIED_ERROR;
167
        }
168
    }
169
}
170