CommentManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 14
Bugs 12 Features 1
Metric Value
wmc 10
c 14
b 12
f 1
lcom 1
cbo 7
dl 0
loc 81
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
C createForm() 0 30 7
A isSpam() 0 14 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Rafidion Michael
5
 * Date: 03/09/2015
6
 * Time: 11:29
7
 */
8
9
namespace Mykees\CommentBundle\Manager;
10
11
use Mykees\CommentBundle\Interfaces\IsCommentable;
12
use Mykees\CommentBundle\Libs\Akismet;
13
use Symfony\Component\Form\FormFactory;
14
use Symfony\Bundle\FrameworkBundle\Routing\Router;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Session\Session;
17
use Mykees\CommentBundle\Form\Type\CommentFormType;
18
19
class CommentManager extends Manager{
20
21
	protected $formFactory;
22
	protected $router;
23
	protected $comment_class;
24
	protected $repository;
25
	protected $session;
26
	protected $formType;
27
28
	public function __construct(FormFactory $formFactory, Router $router, Session $session, CommentFormType $formType, $class)
29
	{
30
		$this->formFactory  = $formFactory;
31
		$this->router       = $router;
32
		$this->comment_class = $class;
33
		$this->formType = $formType;
34
		$this->session = $session;
35
	}
36
37
38
	/**
39
	 * Create Comment Form
40
	 * @param IsCommentable $model
41
	 * @param array $labels
42
	 * @param null $success_message
43
	 * @return \Symfony\Component\Form\Form
44
	 */
45
	public function createForm(IsCommentable $model, $labels = [], $success_message=null)
46
	{
47
		$dataForm = $this->session->get('form_comment_data');
48
		$model_name  = $this->getClassShortName($model);
49
		$route    = $this->router->generate('mykees_comment_manage');
50
		$comment  = new $this->comment_class;
51
		$comment->setModel($model_name);
52
		$comment->setModelId($model->getId());
53
54
		$this->formType->username = !empty($labels['username']) ? $labels['username'] : false;
55
		$this->formType->email    = !empty($labels['email']) ? $labels['email'] : false;
56
		$this->formType->content  = !empty($labels['content']) ? $labels['content'] : false;
57
58
59
		if($this->session->has('success_message')) { $this->session->remove('success_message'); }
60
		if($success_message) { $this->session->set('success_message',$success_message); }
61
62
		$form = $this->formFactory->create(
63
			$this->formType,
0 ignored issues
show
Documentation introduced by
$this->formType is of type object<Mykees\CommentBun...m\Type\CommentFormType>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
			$comment,
65
			['action'=> $route,'method'=> 'POST']
66
		);
67
68
		if( !empty($dataForm) ){
69
			$form->submit($dataForm);
70
			$this->session->remove('form_comment_data');
71
		}
72
73
		return $form;
74
	}
75
76
	/**
77
	 * Verify if the comment is a spam
78
	 * @param $comment
79
	 * @param Request $request
80
	 * @param $akismetInit
81
	 * @return bool
82
	 * @throws \Symfony\Component\Config\Definition\Exception\Exception
83
	 */
84
	public function isSpam($comment, Request $request, $akismetInit)
85
	{
86
		if($akismetInit){
87
			$akismet     = new Akismet($akismetInit['website'],$akismetInit['api_key'],$request);
88
89
			$akismet->setCommentAuthor($comment->getUsername());
90
			$akismet->setCommentAuthorEmail($comment->getEmail());
91
			$akismet->setCommentContent($comment->getContent());
92
			$akismet->setUserIP($comment->getIp());
93
94
			return $akismet->isCommentSpam($request);
95
		}
96
		return false;
97
	}
98
99
}
100