RolloutController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A toggleFeatureAction() 0 12 2
A getRefererUri() 0 9 2
1
<?php
2
/*
3
 * This file is part of the Adlogix package.
4
 *
5
 * (c) Allan Segebarth <[email protected]>
6
 * (c) Jean-Jacques Courtens <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Adlogix\Zf2Rollout\Service\Controller;
13
14
use Opensoft\Rollout\Rollout;
15
use Opensoft\Rollout\RolloutUserInterface;
16
use Zend\Http\Header\Referer;
17
use Zend\Http\Request;
18
use Zend\Mvc\Controller\AbstractActionController;
19
20
/**
21
 * @author Toni Van de Voorde <[email protected]>
22
 */
23
final class RolloutController extends AbstractActionController
24
{
25
    /**
26
     * @var Rollout
27
     */
28
    private $rollout;
29
30
    /**
31
     * @var RolloutUserInterface
32
     */
33
    private $rolloutUser;
34
35
    /**
36
     * @param Rollout              $rollout
37
     * @param RolloutUserInterface $rolloutUser
38
     */
39
    public function __construct(
40
        Rollout $rollout,
41
        RolloutUserInterface $rolloutUser
42
    ) {
43
        $this->rollout = $rollout;
44
        $this->rolloutUser = $rolloutUser;
45
    }
46
47
    /**
48
     * @return \Zend\Http\Response
49
     */
50
    public function toggleFeatureAction()
51
    {
52
        $feature = $this->params()->fromRoute('feature');
53
54
        if ($this->rollout->isActive($feature, $this->rolloutUser)) {
55
            $this->rollout->deactivateUser($feature, $this->rolloutUser);
56
        } else {
57
            $this->rollout->activateUser($feature, $this->rolloutUser);
58
        }
59
60
        return $this->redirect()->toUrl($this->getRefererUri());
61
    }
62
63
    /**
64
     * @return string The referer URI (if not available returns '/' )
65
     */
66
    protected function getRefererUri()
67
    {
68
        /** @var Request $request */
69
        $request = $this->getRequest();
70
71
        $headers = $request->getHeader('Referer', '/');
72
73
        return $headers instanceof Referer ? $headers->getUri() : (string)$headers;
74
    }
75
}