GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#10)
by Daniel
29:39
created

DefaultController::addFlash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Opensoft\RolloutBundle\Controller;
4
5
use Opensoft\Rollout\Rollout;
6
use Opensoft\RolloutBundle\Rollout\UserProviderInterface;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * @author Richard Fullmer <[email protected]>
14
 */
15
class DefaultController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use {@see AbstractController} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
{
17
    /**
18
     * @return Response
19
     */
20
    public function indexAction()
21
    {
22
        return $this->render('OpensoftRolloutBundle:Default:index.html.twig', array('rollout' => $this->getRollout()));
23
    }
24
25
    /**
26
     * @param  string           $feature
27
     * @return RedirectResponse
28
     */
29
    public function activateAction($feature)
30
    {
31
        $this->getRollout()->activate($feature);
32
33
        $this->addFlash('success', sprintf("Feature '%s' is now globally activated", $feature));
34
35
        return $this->redirectToRoute('opensoft_rollout');
36
    }
37
38
    /**
39
     * @param  string           $feature
40
     * @return RedirectResponse
41
     */
42
    public function deactivateAction($feature)
43
    {
44
        $this->getRollout()->deactivate($feature);
45
46
        $this->addFlash('danger', sprintf("Feature '%s' is now globally deactivated", $feature));
47
48
        return $this->redirectToRoute('opensoft_rollout');
49
    }
50
51
    /**
52
     * @param  string           $feature
53
     * @return RedirectResponse
54
     */
55 View Code Duplication
    public function incrementPercentageAction($feature)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $rollout = $this->getRollout();
58
        $percentage = $rollout->get($feature)->getPercentage() + 10;
59
        $rollout->activatePercentage($feature, $percentage);
60
61
        $this->addFlash('info', sprintf("Feature '%s' percentage changed to %d%% of all users", $feature, $percentage));
62
63
        return $this->redirectToRoute('opensoft_rollout');
64
    }
65
66
    /**
67
     * @param  string           $feature
68
     * @return RedirectResponse
69
     */
70 View Code Duplication
    public function decrementPercentageAction($feature)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $rollout = $this->getRollout();
73
        $percentage = $rollout->get($feature)->getPercentage() - 10;
74
        $rollout->activatePercentage($feature, $percentage);
75
76
        $this->addFlash('info', sprintf("Feature '%s' percentage changed to %d%% of all users", $feature, $percentage));
77
78
        return $this->redirectToRoute('opensoft_rollout');
79
    }
80
81
    /**
82
     * @param  string           $feature
83
     * @param  string           $group
84
     * @return RedirectResponse
85
     */
86 View Code Duplication
    public function activateGroupAction($feature, $group)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $this->getRollout()->activateGroup($feature, $group);
89
90
        $this->addFlash('info', sprintf("Feature '%s' is now active in group '%s'", $feature, $group));
91
92
        return $this->redirectToRoute('opensoft_rollout');
93
    }
94
95
    /**
96
     * @param  string           $feature
97
     * @param  string           $group
98
     * @return RedirectResponse
99
     */
100 View Code Duplication
    public function deactivateGroupAction($feature, $group)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $this->getRollout()->deactivateGroup($feature, $group);
103
104
        $this->addFlash('info', sprintf("Feature '%s' is no longer active in group '%s'", $feature, $group));
105
106
        return $this->redirectToRoute('opensoft_rollout');
107
    }
108
109
    /**
110
     * @param  Request          $request
111
     * @param  string           $feature
112
     * @return RedirectResponse
113
     */
114
    public function activateUserAction(Request $request, $feature)
115
    {
116
        $requestUser = $request->get('user');
117
        $user = $this->getRolloutUserProvider()->findByRolloutIdentifier($requestUser);
118
119
        if ($user) {
120
            $this->getRollout()->activateUser($feature, $user);
121
122
            $this->addFlash('info', sprintf("User '%s' was activated in feature '%s'", $user->getRolloutIdentifier(), $feature));
123
        } else {
124
            $this->addFlash('danger', sprintf("User '%s' not found", $requestUser));
125
        }
126
127
        return $this->redirectToRoute('opensoft_rollout');
128
    }
129
130
    /**
131
     * @param  string           $feature
132
     * @param  string           $id
133
     * @return RedirectResponse
134
     */
135
    public function deactivateUserAction($feature, $id)
136
    {
137
        $user = $this->getRolloutUserProvider()->findByRolloutIdentifier($id);
138
        $this->getRollout()->deactivateUser($feature, $user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->getRolloutUserPro...yRolloutIdentifier($id) on line 137 can be null; however, Opensoft\Rollout\Rollout::deactivateUser() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
139
140
        $this->addFlash('info', sprintf("User '%s' was deactivated from feature '%s'", $user->getRolloutIdentifier(), $feature));
141
142
        return $this->redirectToRoute('opensoft_rollout');
143
    }
144
145
    /**
146
     * @param  string           $feature
147
     * @return RedirectResponse
148
     */
149
    public function removeAction($feature)
150
    {
151
        $this->getRollout()->remove($feature);
152
153
        $this->addFlash('info', sprintf("Feature '%s' was removed from rollout.", $feature));
154
155
        return $this->redirectToRoute('opensoft_rollout');
156
    }
157
158
    /**
159
     * @param Request $request
160
     * @param string $feature
161
     * @return RedirectResponse
162
     */
163
    public function setRequestParamAction(Request $request, $feature)
164
    {
165
        $requestParam = $request->get('requestParam');
166
        if ($requestParam === null) {
167
            $this->addFlash('danger', 'Missing "requestParam" value');
168
            return $this->redirectToRoute('opensoft_rollout');
169
        }
170
171
        $this->getRollout()->activateRequestParam($feature, $requestParam);
172
173
        $this->addFlash('info', sprintf('Feature "%s" requestParam changed to "%s"', $feature, $requestParam));
174
175
        return $this->redirectToRoute('opensoft_rollout');
176
    }
177
178
    /**
179
     * @return Rollout
180
     */
181
    private function getRollout()
182
    {
183
        return $this->container->get('rollout');
184
    }
185
186
    /**
187
     * @return UserProviderInterface
188
     */
189
    private function getRolloutUserProvider()
190
    {
191
        return $this->container->get('rollout.user_provider');
192
    }
193
}
194