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
Push — master ( b18b5e...34ec0d )
by Richard
04:26
created

createRedirectToFeatureListReponse()   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 0
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\DependencyInjection\ContainerAware;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * @author Richard Fullmer <[email protected]>
15
 */
16
class DefaultController extends ContainerAware
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...njection\ContainerAware has been deprecated with message: since version 2.8, to be removed in 3.0. Use the ContainerAwareTrait 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...
17
{
18
    /**
19
     * @return Response
20
     */
21
    public function indexAction()
22
    {
23
        return $this->container->get('templating')->renderResponse('OpensoftRolloutBundle:Default:index.html.twig', array('rollout' => $this->getRollout()));
24
    }
25
26
    /**
27
     * @param  string           $feature
28
     * @return RedirectResponse
29
     */
30
    public function activateAction($feature)
31
    {
32
        $this->getRollout()->activate($feature);
33
34
        $this->addFlash('success', sprintf("Feature '%s' is now globally activated", $feature));
35
36
        return $this->createRedirectToFeatureListReponse();
37
    }
38
39
    /**
40
     * @param  string           $feature
41
     * @return RedirectResponse
42
     */
43
    public function deactivateAction($feature)
44
    {
45
        $this->getRollout()->deactivate($feature);
46
47
        $this->addFlash('danger', sprintf("Feature '%s' is now globally deactivated", $feature));
48
49
        return $this->createRedirectToFeatureListReponse();
50
    }
51
52
    /**
53
     * @param  string           $feature
54
     * @return RedirectResponse
55
     */
56 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...
57
    {
58
        $rollout = $this->getRollout();
59
        $percentage = $rollout->get($feature)->getPercentage() + 10;
60
        $rollout->activatePercentage($feature, $percentage);
61
62
        $this->addFlash('info', sprintf("Feature '%s' percentage changed to %d%% of all users", $feature, $percentage));
63
64
        return $this->createRedirectToFeatureListReponse();
65
    }
66
67
    /**
68
     * @param  string           $feature
69
     * @return RedirectResponse
70
     */
71 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...
72
    {
73
        $rollout = $this->getRollout();
74
        $percentage = $rollout->get($feature)->getPercentage() - 10;
75
        $rollout->activatePercentage($feature, $percentage);
76
77
        $this->addFlash('info', sprintf("Feature '%s' percentage changed to %d%% of all users", $feature, $percentage));
78
79
        return $this->createRedirectToFeatureListReponse();
80
    }
81
82
    /**
83
     * @param  string           $feature
84
     * @param  string           $group
85
     * @return RedirectResponse
86
     */
87 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...
88
    {
89
        $this->getRollout()->activateGroup($feature, $group);
90
91
        $this->addFlash('info', sprintf("Feature '%s' is now active in group '%s'", $feature, $group));
92
93
        return $this->createRedirectToFeatureListReponse();
94
    }
95
96
    /**
97
     * @param  string           $feature
98
     * @param  string           $group
99
     * @return RedirectResponse
100
     */
101 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...
102
    {
103
        $this->getRollout()->deactivateGroup($feature, $group);
104
105
        $this->addFlash('info', sprintf("Feature '%s' is no longer active in group '%s'", $feature, $group));
106
107
        return $this->createRedirectToFeatureListReponse();
108
    }
109
110
    /**
111
     * @param  Request          $request
112
     * @param  string           $feature
113
     * @return RedirectResponse
114
     */
115
    public function activateUserAction(Request $request, $feature)
116
    {
117
        $requestUser = $request->get('user');
118
        $user = $this->getRolloutUserProvider()->findByRolloutIdentifier($requestUser);
119
120
        if ($user) {
121
            $this->getRollout()->activateUser($feature, $user);
122
123
            $this->addFlash('info', sprintf("User '%s' was activated in feature '%s'", $user->getRolloutIdentifier(), $feature));
124
        } else {
125
            $this->addFlash('danger', sprintf("User '%s' not found", $requestUser));
126
        }
127
128
        return $this->createRedirectToFeatureListReponse();
129
    }
130
131
    /**
132
     * @param  string           $feature
133
     * @param  string           $id
134
     * @return RedirectResponse
135
     */
136
    public function deactivateUserAction($feature, $id)
137
    {
138
        $user = $this->getRolloutUserProvider()->findByRolloutIdentifier($id);
139
        $this->getRollout()->deactivateUser($feature, $user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->getRolloutUserPro...yRolloutIdentifier($id) on line 138 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...
140
141
        $this->addFlash('info', sprintf("User '%s' was deactivated from feature '%s'", $user->getRolloutIdentifier(), $feature));
142
143
        return $this->createRedirectToFeatureListReponse();
144
    }
145
146
    /**
147
     * @param  string           $feature
148
     * @return RedirectResponse
149
     */
150
    public function removeAction($feature)
151
    {
152
        $this->getRollout()->remove($feature);
153
154
        $this->addFlash('info', sprintf("Feature '%s' was removed from rollout.", $feature));
155
156
        return $this->createRedirectToFeatureListReponse();
157
    }
158
159
    /**
160
     * @param Request $request
161
     * @param string $feature
162
     * @return RedirectResponse
163
     */
164
    public function setRequestParamAction(Request $request, $feature)
165
    {
166
        $requestParam = $request->get('requestParam');
167
        if ($requestParam === null) {
168
            $this->addFlash('danger', 'Missing "requestParam" value');
169
            return $this->createRedirectToFeatureListReponse();
170
        }
171
172
        $this->getRollout()->activateRequestParam($feature, $requestParam);
173
174
        $this->addFlash('info', sprintf('Feature "%s" requestParam changed to "%s"', $feature, $requestParam));
175
176
        return $this->createRedirectToFeatureListReponse();
177
    }
178
179
    /**
180
     * @return Rollout
181
     */
182
    private function getRollout()
183
    {
184
        return $this->container->get('rollout');
185
    }
186
187
    /**
188
     * @return UserProviderInterface
189
     */
190
    private function getRolloutUserProvider()
191
    {
192
        return $this->container->get('rollout.user_provider');
193
    }
194
195
    /**
196
     * Helper for adding flash messages
197
     *
198
     * @param string $type
199
     * @param string $message
200
     */
201
    private function addFlash($type, $message)
202
    {
203
        $this->container->get('session')->getFlashBag()->add($type, $message);
204
    }
205
206
    /**
207
     * @return RedirectResponse
208
     */
209
    private function createRedirectToFeatureListReponse()
210
    {
211
        return new RedirectResponse($this->container->get('router')->generate('opensoft_rollout'));
212
    }
213
}
214