Completed
Push — master ( ca1336...ce2897 )
by Adrien
07:43
created

MovieController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 6
Bugs 2 Features 1
Metric Value
cc 1
eloc 6
c 6
b 2
f 1
nc 1
nop 0
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
1
<?php
2
3
class MovieController extends Zend_Controller_Action
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5 3
    public function init()
6
    {
7
        // Init the Context Switch Action helper
8 3
        $contextSwitch = $this->_helper->contextSwitch();
9
10
        // Add the new context
11 3
        $contextSwitch->setContexts([
12 3
            'csv' => ['suffix' => 'csv'],
13
            'rss' => ['suffix' => 'rss'],
14
        ]);
15
16 3
        $contextSwitch->addActionContext('index', 'csv')->addActionContext('index', 'rss')->initContext();
17 3
    }
18
19 1
    public function indexAction()
20
    {
21
        // Check there is at least one user, otherwise the whole page will crash
22 1
        if (!\mQueue\Model\User::getCurrent() && !\mQueue\Model\UserMapper::getDbTable()->fetchRow()) {
23
            throw new Exception('At least one user must exist to access this page');
24
        }
25
26 1
        $form = new \mQueue\Form\Filters();
27 1
        $this->view->formFilter = $form;
1 ignored issue
show
Bug introduced by
Accessing formFilter on the interface Zend_View_Interface 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...
28
29
        // Detect if at least one filter was submitted
30 1
        $submitted = false;
31 1
        foreach ($this->getRequest()->getParams() as $key => $filter) {
32 1
            if (preg_match('/^filter\d+$/', $key)) {
33 1
                $submitted = true;
34
            }
35
        }
36
37
        // If was submitted and do not want to clear, try to validate values
38 1
        if ($submitted && !$this->_getParam('clear', false)) {
39
            if (!$form->isValid($this->getRequest()->getParams())) {
40
                $this->_helper->FlashMessenger(['warning' => _tr('Filter is invalid.')]);
41
                $form->setDefaults([]);
42
            }
43
        }
44
        // If we submitted a quicksearch, set default values to search with any status
45 1
        elseif ($this->_getParam('search')) {
46
            $form->setDefaults([
47
                'filter1' => [
48
                    'user' => \mQueue\Model\User::getCurrent() ? 0 : \mQueue\Model\UserMapper::fetchAll()->current()->id,
1 ignored issue
show
Bug introduced by
The method current cannot be called on \mQueue\Model\UserMapper::fetchAll() (of type array<integer,object<mQueue\Model\User>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
49
                    'status' => -2,
50
                    'title' => $this->_getParam('search'),
51
                ],
52
            ]);
53
        }
54
        // Otherwise clear the filter
55
        else {
56 1
            $form->setDefaults([]);
57
        }
58
59
        // Gather users selected in filters
60 1
        $this->view->users = [];
1 ignored issue
show
Bug introduced by
Accessing users on the interface Zend_View_Interface 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...
61 1
        $filters = $form->getValues();
62 1
        foreach ($filters as $key => $filter) {
63 1
            if (!preg_match('/^filter\d+$/', $key)) {
64 1
                continue;
65
            }
66
67 1
            $this->view->users[$filter['user']] = \mQueue\Model\UserMapper::find($filter['user']);
1 ignored issue
show
Bug introduced by
Accessing users on the interface Zend_View_Interface 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...
68
        }
69
70
        // If we ouput rss, we force sorting by date
71 1
        if ($this->_helper->contextSwitch()->getCurrentContext() == 'rss') {
72
            $this->getRequest()->setParam('sort', $filters['filter1']['withSource'] ? 'dateSearch' : 'date');
73
            $this->getRequest()->setParam('sortOrder', 'desc');
74
        }
75 1
        $this->view->permanentParams = $form->getValues();
1 ignored issue
show
Bug introduced by
Accessing permanentParams on the interface Zend_View_Interface 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 1
        $this->view->filterName = $form->getValuesText();
1 ignored issue
show
Bug introduced by
Accessing filterName on the interface Zend_View_Interface 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...
77 1
        unset($this->view->permanentParams['addFilter']);
78
79 1
        $allowedSortingKey = ['title', 'date', 'dateSearch'];
80 1
        $usersCount = count($this->view->users);
1 ignored issue
show
Bug introduced by
Accessing users on the interface Zend_View_Interface 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 1
        for ($i = 0; $i < $usersCount; ++$i) {
82 1
            $allowedSortingKey[] = 'status' . $i;
83
        }
84 1
        $sort = $this->_helper->createSorting('sort', $allowedSortingKey);
85
86
        // Set up the paginator: Apply pagination only if there is no special context (so it is normal html rendering)
87 1
        $this->view->paginator = $this->_helper->createPaginator(\mQueue\Model\MovieMapper::getFilteredQuery($filters, $sort));
1 ignored issue
show
Bug introduced by
Accessing paginator on the interface Zend_View_Interface 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...
88 1
    }
89
90 View Code Duplication
    public function viewAction()
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...
91
    {
92
        if ($this->getRequest()->getParam('id')) {
93
            $this->view->movie = \mQueue\Model\MovieMapper::find($this->getRequest()->getParam('id'));
1 ignored issue
show
Bug introduced by
Accessing movie on the interface Zend_View_Interface 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
        }
95
96
        if (!$this->view->movie) {
1 ignored issue
show
Bug introduced by
Accessing movie on the interface Zend_View_Interface 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
            throw new Exception($this->view->translate('Movie not found'));
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend_View_Interface as the method translate() does only exist in the following implementations of said interface: Zend_View.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
98
        }
99
100
        $this->view->users = \mQueue\Model\UserMapper::fetchAll();
1 ignored issue
show
Bug introduced by
Accessing users on the interface Zend_View_Interface 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...
101
        $this->view->movieActivity = $this->_helper->createPaginator(\mQueue\Model\StatusMapper::getActivityQuery($this->view->movie));
2 ignored issues
show
Bug introduced by
Accessing movieActivity on the interface Zend_View_Interface 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
Accessing movie on the interface Zend_View_Interface 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
    }
103
104 1
    public function addAction()
105
    {
106 1
        $request = $this->getRequest();
107 1
        $form = new \mQueue\Form\Movie();
108
109 1
        if ($this->_getParam('id')) {
110
            if ($form->isValid($request->getParams())) {
111
                $values = $form->getValues();
112
                $movie = \mQueue\Model\MovieMapper::find(\mQueue\Model\Movie::extractId($values['id']));
113
                if (!$movie) {
114
                    $movie = \mQueue\Model\MovieMapper::getDbTable()->createRow();
115
                    $movie->setId($values['id']);
116
                    $movie->save();
117
                    $this->_helper->FlashMessenger(_tr('A movie was added.'));
118
                }
119
120
                $this->view->movies = [$movie];
1 ignored issue
show
Bug introduced by
Accessing movies on the interface Zend_View_Interface 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...
121
            }
122
        }
123
124 1
        $this->view->form = $form;
1 ignored issue
show
Bug introduced by
Accessing form on the interface Zend_View_Interface 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...
125 1
    }
126
127 1
    public function importAction()
128
    {
129 1
        $request = $this->getRequest();
130 1
        $form = new \mQueue\Form\Import();
131 1
        $form->setDefaults(['favoriteMinimum' => 9, 'excellentMinimum' => 7, 'okMinimum' => 5]);
132 1
        $this->view->form = $form;
1 ignored issue
show
Bug introduced by
Accessing form on the interface Zend_View_Interface 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...
133
134 1
        if ($this->getRequest()->isPost() && $form->isValid($request->getPost())) {
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Zend_Controller_Request_Abstract as the method isPost() does only exist in the following sub-classes of Zend_Controller_Request_Abstract: Zend_Controller_Request_Apache404, Zend_Controller_Request_Http, Zend_Controller_Request_HttpTestCase. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
135
            if (\mQueue\Model\User::getCurrent() == null) {
136
                $this->_helper->FlashMessenger(['error' => _tr('You must be logged in.')]);
137
138
                return;
139
            }
140
141
            $values = $form->getValues();
142
            $page = file_get_contents($values['url']);
143
144
            $pattern = '|<a href="/title/tt(\d{7})/">.*</td>\s*<td.*>(\d+(\.\d)*)</td>|U';
145
            preg_match_all($pattern, $page, $matches);
146
147
            $movies = [];
148
            $matchesCount = count($matches[1]);
149
            for ($i = 0; $i < $matchesCount; ++$i) {
150
                $id = $matches[1][$i];
151
                $imdbRating = $matches[2][$i];
152
153
                $movie = \mQueue\Model\MovieMapper::find($id);
154
                if (!$movie) {
155
                    $movie = \mQueue\Model\MovieMapper::getDbTable()->createRow();
156
                    $movie->setId($id);
157
                    $movie->save();
158
                }
159
160
                if ($imdbRating >= $values['favoriteMinimum']) {
161
                    $rating = \mQueue\Model\Status::Favorite;
162
                } elseif ($imdbRating >= $values['excellentMinimum']) {
163
                    $rating = \mQueue\Model\Status::Excellent;
164
                } elseif ($imdbRating >= $values['okMinimum']) {
165
                    $rating = \mQueue\Model\Status::Ok;
166
                } else {
167
                    $rating = \mQueue\Model\Status::Bad;
168
                }
169
170
                $movie->setStatus(\mQueue\Model\User::getCurrent(), $rating);
171
                $movies [] = $movie;
172
            }
173
174
            $count = count($movies);
175
            if ($count) {
176
                $this->_helper->FlashMessenger(_tr('Movies imported.'));
177
                $this->view->movies = $movies;
1 ignored issue
show
Bug introduced by
Accessing movies on the interface Zend_View_Interface 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...
178
            } else {
179
                $this->_helper->FlashMessenger(['warning' => _tr('No movies found for import.')]);
180
            }
181
        }
182 1
    }
183
}
184