Completed
Push — devel ( b03b2f...7508db )
by Philippe
05:13
created

ClientController::actionUpdate()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
ccs 0
cts 26
cp 0
rs 8.439
cc 5
eloc 22
nc 9
nop 1
crap 30
1
<?php
2
/**
3
 * ClientController.php
4
 *
5
 * PHP version 5.6+
6
 *
7
 * @author pgaultier
8
 * @copyright 2010-2016 Ibitux
9
 * @license http://www.ibitux.com/license license
10
 * @version XXX
11
 * @link http://www.ibitux.com
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
21
/**
22
 * Manage oauth clients
23
 *
24
 * @author pgaultier
25
 * @copyright 2010-2016 Ibitux
26
 * @license http://www.ibitux.com/license license
27
 * @version XXX
28
 * @link http://www.ibitux.com
29
 * @package sweelix\oauth2\server\commands
30
 * @since XXX
31
 */
32
class ClientController extends Controller
33
{
34
35
    public $redirectUri;
36
    public $grantTypes;
37
    public $scopes;
38
    public $userId;
39
    public $name;
40
    public $isPublic;
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function options($actionID)
46
    {
47
        return [
48
            // Generated 'id',
49
            // Generated 'secret',
50
            'redirectUri',
51
            'grantTypes',
52
            'scopes',
53
            'userId',
54
            'name',
55
            'isPublic'
56
        ];
57
    }
58
    /**
59
     * Create new Oauth client
60
     * @return int
61
     * @since XXX
62
     */
63
    public function actionCreate()
64
    {
65
66
        $client = Yii::createObject('sweelix\oauth2\server\interfaces\ClientModelInterface');
67
        /* @var \sweelix\oauth2\server\interfaces\ClientModelInterface $client */
68
        $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...
69
        $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...
70
        $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...
71
        $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...
72
        $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...
73
        $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...
74
        $client->scopes = empty($this->scope) ? 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...
Bug introduced by
The property scope does not seem to exist. Did you mean scopes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
75
        $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...
76
        if ($client->save() === true) {
77
            $this->stdout('Client created :'."\n");
78
            $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...
79
            $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...
80
            $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...
81
            $this->stdout(' - redirectUri: ' . $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...
82
            return Controller::EXIT_CODE_NORMAL;
83
        } else {
84
            $this->stdout('Client cannot be created.'."\n");
85
            return Controller::EXIT_CODE_ERROR;
86
        }
87
    }
88
89
    public function actionUpdate($id)
90
    {
91
        $client = Client::findOne($id);
92
        if ($client !== null) {
93
            $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...
94
            $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...
95
            $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...
96
            $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...
97
            $client->scopes = empty($this->scope) ? 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...
Bug introduced by
The property scope does not seem to exist. Did you mean scopes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
98
            $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...
99
            if ($client->save() === true) {
100
                $this->stdout('Client updated :' . "\n");
101
                $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...
102
                $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...
103
                $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...
104
                $this->stdout(' - redirectUri: ' . $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...
105
                return Controller::EXIT_CODE_NORMAL;
106
            } else {
107
                $this->stdout('Client cannot be updated.'."\n");
108
                return Controller::EXIT_CODE_ERROR;
109
            }
110
        } else {
111
            $this->stdout('Client '.$id.' does not exist'."\n");
112
            return Controller::EXIT_CODE_ERROR;
113
        }
114
    }
115
116
    /**
117
     * Generate random string
118
     * @param int $length
119
     * @return string
120
     * @since XXX
121
     */
122
    protected function getRandomString($length = 40)
123
    {
124
        $bytes = (int) $length/2;
125
        return bin2hex(openssl_random_pseudo_bytes($bytes));
126
    }
127
}
128