Failed Conditions
Push — master ( 2b3b32...674f38 )
by Adrien
03:18
created

StatusController::indexAction()   B

Complexity

Conditions 7
Paths 14

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 33.8395

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 35
ccs 4
cts 22
cp 0.1818
rs 8.6186
c 4
b 0
f 0
cc 7
nc 14
nop 0
crap 33.8395
1
<?php
2
3
class StatusController extends Zend_Controller_Action
4
{
5 1
    public function init(): void
6
    {
7 1
        $contextSwitch = $this->_helper->getHelper('contextSwitch');
8 1
        $contextSwitch->addActionContext('list', 'json')
0 ignored issues
show
Bug introduced by
The method addActionContext() does not exist on Zend_Controller_Action_Helper_Abstract. It seems like you code against a sub-type of Zend_Controller_Action_Helper_Abstract such as Zend_Controller_Action_Helper_Cache or Zend_Controller_Action_Helper_ContextSwitch or Zend_Controller_Action_Helper_Redirector or Zend_Layout_Controller_Action_Helper_Layout. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

8
        $contextSwitch->/** @scrutinizer ignore-call */ 
9
                        addActionContext('list', 'json')
Loading history...
9 1
                ->addActionContext('index', 'json')
10 1
                ->initContext();
11 1
    }
12
13 1
    public function indexAction(): void
14
    {
15 1
        $jsonCallback = $this->_request->getParam('jsoncallback');
16 1
        if ($jsonCallback) {
17
            $this->_helper->layout->setLayout('jsonp');
0 ignored issues
show
Bug introduced by
The method setLayout() does not exist on Zend_Controller_Action_Helper_Abstract. It seems like you code against a sub-type of Zend_Controller_Action_Helper_Abstract such as Zend_Controller_Action_Helper_Cache or Zend_Controller_Action_Helper_Redirector or Zend_Layout_Controller_Action_Helper_Layout. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
            $this->_helper->layout->/** @scrutinizer ignore-call */ 
18
                                    setLayout('jsonp');
Loading history...
18
            $this->view->jsonCallback = $jsonCallback;
19
        }
20
21 1
        $idMovie = \mQueue\Model\Movie::extractId($this->_request->getParam('movie'));
0 ignored issues
show
Bug introduced by
It seems like $this->_request->getParam('movie') can also be of type null; however, parameter $string of mQueue\Model\Movie::extractId() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

21
        $idMovie = \mQueue\Model\Movie::extractId(/** @scrutinizer ignore-type */ $this->_request->getParam('movie'));
Loading history...
22
23
        if ($idMovie == null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $idMovie of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
24
            throw new Exception('no valid movie specified.');
25
        }
26
27
        // If new rating is specified and we are logged in, save it and create movie if needed
28
        $rating = $this->_request->getParam('rating');
29
        if (isset($rating) && \mQueue\Model\User::getCurrent()) {
30
            $movie = \mQueue\Model\MovieMapper::find($idMovie);
0 ignored issues
show
Bug introduced by
$idMovie of type string is incompatible with the type integer expected by parameter $id of mQueue\Model\MovieMapper::find(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            $movie = \mQueue\Model\MovieMapper::find(/** @scrutinizer ignore-type */ $idMovie);
Loading history...
31
32
            if ($movie == null) {
33
                $movie = \mQueue\Model\MovieMapper::getDbTable()->createRow();
34
                $movie->setId($idMovie);
35
                $movie->save();
36
            }
37
            $status = $movie->setStatus(\mQueue\Model\User::getCurrent(), $rating);
38
        } else {
39
            $status = \mQueue\Model\StatusMapper::find($idMovie, \mQueue\Model\User::getCurrent());
0 ignored issues
show
Bug introduced by
$idMovie of type string is incompatible with the type integer expected by parameter $idMovie of mQueue\Model\StatusMapper::find(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
            $status = \mQueue\Model\StatusMapper::find(/** @scrutinizer ignore-type */ $idMovie, \mQueue\Model\User::getCurrent());
Loading history...
40
        }
41
42
        if (!$jsonCallback) {
43
            $this->view->status = $status;
44
        } else {
45
            $html = $this->view->statusLinks($status);
46
            $this->view->status = $html;
47
            $this->view->id = $status->getUniqueId();
0 ignored issues
show
Bug introduced by
The method getUniqueId() does not exist on Zend_Db_Table_Rowset_Abstract. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            /** @scrutinizer ignore-call */ 
48
            $this->view->id = $status->getUniqueId();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
        }
49
    }
50
51
    public function listAction(): void
52
    {
53
        $jsonCallback = $this->_request->getParam('jsoncallback');
54
        if ($jsonCallback) {
55
            $this->_helper->layout->setLayout('jsonp');
56
            $this->view->jsonCallback = $jsonCallback;
57
        }
58
59
        $idMovies = [];
60
        foreach (explode(',', trim($this->_request->getParam('movies'), ',')) as $idMovie) {
61
            $idMovie = \mQueue\Model\Movie::extractId($idMovie);
62
            if ($idMovie) {
63
                $idMovies[] = $idMovie;
64
            }
65
        }
66
67
        $statuses = \mQueue\Model\StatusMapper::findAll($idMovies, \mQueue\Model\User::getCurrent());
68
69
        $json = [];
70
        foreach ($statuses as $s) {
71
            $html = $this->view->statusLinks($s);
72
            $json[$s->getUniqueId()] = $html;
73
        }
74
75
        $this->view->status = $json;
76
    }
77
78
    public function graphAction(): void
79
    {
80
        $percent = $this->_request->getParam('percent');
81
        $user = \mQueue\Model\UserMapper::find($this->getParam('user'));
82
        $data = \mQueue\Model\StatusMapper::getGraph($user, $percent);
83
        $chart = [
84
            'chart' => [
85
                'zoomType' => 'x',
86
            ],
87
            'title' => [
88
                'text' => '',
89
            ],
90
            'xAxis' => [
91
                'type' => 'datetime',
92
            ],
93
            'yAxis' => [
94
                'title' => [
95
                    'text' => 'Movies',
96
                ],
97
                'min' => 0,
98
            ],
99
            'series' => $data,
100
        ];
101
102
        if ($percent) {
103
            $chart['chart']['type'] = 'area';
104
            $chart['yAxis']['title']['text'] = $chart['yAxis']['title']['text'] . ' [%]';
105
            $chart['plotOptions'] = [
106
                'area' => [
107
                    'stacking' => 'percent',
108
                    'marker' => [
109
                        'enabled' => false,
110
                    ],
111
                ],
112
            ];
113
        }
114
115
        echo Zend_Json::encode($chart, Zend_Json::TYPE_ARRAY);
0 ignored issues
show
Bug introduced by
Zend_Json::TYPE_ARRAY of type integer is incompatible with the type boolean expected by parameter $cycleCheck of Zend_Json::encode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        echo Zend_Json::encode($chart, /** @scrutinizer ignore-type */ Zend_Json::TYPE_ARRAY);
Loading history...
116
        die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
117
    }
118
}
119