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.

CommentController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
namespace RbComment\Controller;
3
4
use RbComment\Model\Comment;
5
use RbComment\Form\CommentForm;
6
use RbComment\Model\CommentTable;
7
use Zend\Http\PhpEnvironment\RemoteAddress;
8
use Zend\Mvc\Controller\AbstractActionController;
9
use ZendService\Akismet\Akismet;
10
11
class CommentController extends AbstractActionController
12
{
13
    /**
14
     * @var CommentTable
15
     */
16
    private $commentTable;
17
18
    /**
19
     * @var Akismet
20
     */
21
    private $akismetService;
22
23
    /**
24
     * @var array
25
     */
26
    private $configService;
27
28
    public function __construct(
29
        array $configService,
30
        CommentTable $commentTable,
31
        Akismet $akismetService
32
    ) {
33
        $this->configService  = $configService;
34
        $this->commentTable   = $commentTable;
35
        $this->akismetService = $akismetService;
36
    }
37
38
    public function addAction()
39
    {
40
        $rbCommentConfig = (object) $this->configService['rb_comment'];
41
42
        $form = new CommentForm($rbCommentConfig->strings);
43
44
        $request = $this->getRequest();
45
        if ($request->isPost()) {
46
            $comment = new Comment();
47
            $form->setInputFilter($comment->getInputFilter());
48
            $form->setData($request->getPost());
49
50
            if ($form->isValid()) {
51
                $comment->exchangeArray($form->getData());
52
53
                // Set default visibility from config
54
                $comment->visible = $rbCommentConfig->default_visibility;
55
56
                // If akismet is enabled check for spam
57
                if (($rbCommentConfig->akismet['enabled'] === true) &&
58
                    $this->isSpam($comment, $rbCommentConfig)) {
59
                    $comment->spam = 1;
0 ignored issues
show
Documentation Bug introduced by
The property $spam was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
60
                    $comment->visible = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $visible was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
61
                }
62
63
                // We need the id for the mailer
64
                $comment->id = $this->commentTable->saveComment($comment);
65
66
                // Send email if active and not spam
67
                if (($rbCommentConfig->email['notify'] === true) &&
68
                    ($comment->spam === 0)) {
69
                    $this->rbMailer($comment);
0 ignored issues
show
Documentation Bug introduced by
The method rbMailer does not exist on object<RbComment\Controller\CommentController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
70
                }
71
72
                return $this->redirect()->toUrl($form->get('uri')->getValue());
73
            } else {
74
                $this->flashMessenger()->setNamespace('RbComment');
0 ignored issues
show
Documentation Bug introduced by
The method flashMessenger does not exist on object<RbComment\Controller\CommentController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
                $this->flashMessenger()->addMessage(json_encode($form->getMessages()));
0 ignored issues
show
Documentation Bug introduced by
The method flashMessenger does not exist on object<RbComment\Controller\CommentController>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
76
77
                return $this->redirect()->toUrl($form->get('uri')->getValue() . '#rbcomment');
78
            }
79
        }
80
    }
81
82
    /**
83
     * Checks if a comment is spam using the akismet service.
84
     *
85
     * @param  Comment $comment
86
     * @param  mixed   $rbCommentConfig
87
     * @return boolean
88
     */
89
    protected function isSpam($comment, $rbCommentConfig)
90
    {
91
        $remote = new RemoteAddress();
92
        $remote->setUseProxy($rbCommentConfig->akismet['proxy']['use']);
93
        $remote->setTrustedProxies($rbCommentConfig->akismet['proxy']['trusted']);
94
        $remote->setProxyHeader($rbCommentConfig->akismet['proxy']['header']);
95
96
        $content = [
97
            'user_ip'              => $remote->getIpAddress(),
98
            'user_agent'           => filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'),
99
            'comment_type'         => 'comment',
100
            'comment_author'       => $comment->author,
101
            'comment_author_email' => $comment->contact,
102
            'comment_content'      => $comment->content,
103
        ];
104
105
        return $this->akismetService->isSpam($content);
106
    }
107
}
108