Completed
Push — master ( 6f92e6...cbba0d )
by Fèvre
9s
created

PollsController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 6
dl 0
loc 71
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C vote() 0 62 8
1
<?php
2
namespace App\Controller;
3
4
use Cake\I18n\Time;
5
use Cake\Network\Exception\NotFoundException;
6
7
class PollsController extends AppController
8
{
9
10
    /**
11
     * An user vote to a poll.
12
     *
13
     * @return \Cake\Network\Exception\NotFoundException|\Cake\Network\Response
14
     */
15
    public function vote()
16
    {
17
        if (!$this->request->is('post') || !isset($this->request->data['answer_id'])) {
18
            throw new NotFoundException();
19
        }
20
21
        $this->loadModel('PollsAnswers');
22
        $answer = $this->PollsAnswers
0 ignored issues
show
Documentation introduced by
The property PollsAnswers does not exist on object<App\Controller\PollsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
23
            ->find()
24
            ->contain([
25
                'Polls'
26
            ])
27
            ->where([
28
                'PollsAnswers.id' => $this->request->data['answer_id'],
29
                'Polls.article_id' => $this->request->id
30
            ])
31
            ->first();
32
33
        if (is_null($answer)) {
34
            $this->Flash->error(__('This answer doesn\'t exist.'));
35
36
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
37
        }
38
39
        if ($answer->poll->is_timed == true && $answer->poll->end_date < new Time()) {
40
            $this->Flash->error(__('This poll is expired, you can not vote anymore.'));
41
42
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
43
        }
44
45
        $this->loadModel('PollsUsers');
46
        $hasVoted = $this->PollsUsers
0 ignored issues
show
Documentation introduced by
The property PollsUsers does not exist on object<App\Controller\PollsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47
            ->find()
48
            ->contain([
49
                'PollsAnswers'
50
            ])
51
            ->where([
52
                'PollsUsers.poll_id' => $answer->poll->id,
53
                'PollsUsers.user_id' => $this->Auth->user('id')
54
            ])
55
            ->first();
56
57
        if (!is_null($hasVoted)) {
58
            $this->Flash->error(__('You have already voted for this poll ! (You voted <strong>{0}</strong>).', h($hasVoted->polls_answer->response)));
59
60
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
        }
62
63
        $this->request->data['poll_id'] = $answer->poll->id;
64
        $this->request->data['user_id'] = $this->Auth->user('id');
65
        $user = $this->PollsUsers->newEntity($this->request->data);
0 ignored issues
show
Documentation introduced by
The property PollsUsers does not exist on object<App\Controller\PollsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
66
67
        if ($this->PollsUsers->save($user)) {
0 ignored issues
show
Documentation introduced by
The property PollsUsers does not exist on object<App\Controller\PollsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
            $this->Flash->success(__('Your have successfully voted for this poll ! (You voted <strong>{0}</strong>).', h($answer->response)));
69
70
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
71
        }
72
73
        $this->Flash->error(__('An error occured while saving the vote !'));
74
75
        return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
76
    }
77
}
78