Passed
Push — master ( 6c1980...4e24b5 )
by Alexey
03:34
created

CommentFactory::validateData()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 7.7777
cc 8
eloc 11
nc 3
nop 1
crap 72
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Skobkin\Bundle\PointToolsBundle\Entity\Blogs\Comment;
8
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\ApiException;
9
use Skobkin\Bundle\PointToolsBundle\Service\Exceptions\InvalidResponseException;
10
use Skobkin\Bundle\PointToolsBundle\Service\Factory\UserFactory;
11
12
13
class CommentFactory
14
{
15
    /**
16
     * @var EntityManager
17
     */
18
    private $em;
19
20
    /**
21
     * @var EntityRepository
22
     */
23
    private $commentRepository;
24
25
    /**
26
     * @var EntityRepository
27
     */
28
    private $postRepository;
29
30
    /**
31
     * @var UserFactory
32
     */
33
    private $userFactory;
34
35
    /**
36
     * @param EntityManager $em
37
     * @param UserFactory $userFactory
38
     */
39
    public function __construct(EntityManager $em, UserFactory $userFactory)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
40
    {
41
        $this->em = $em;
42
        $this->userFactory = $userFactory;
43
        $this->commentRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Comment');
44
        $this->postRepository = $em->getRepository('SkobkinPointToolsBundle:Blogs\Post');
45
    }
46
47
    /**
48
     * @param array $data
49
     *
50
     * @return Comment
51
     * @throws ApiException
52
     * @throws InvalidResponseException
53
     */
54
    public function createFromArray(array $data)
55
    {
56
        $this->validateData($data);
57
58
        if (null === ($comment = $this->commentRepository->find(['post' => $data['post_id'], 'id' => $data['id']]))) {
59
            // @fixme rare non-existing post bug
60
            $post = $this->postRepository->find($data['post_id']);
61
            $author = $this->userFactory->createFromArray($data['author']);
62
            if (null !== $data['to_comment_id']) {
63
                $toComment = $this->commentRepository->find(['post' => $data['post_id'], 'id' => $data['to_comment_id']]);
64
            } else {
65
                $toComment = null;
66
            }
67
            $createdAt = new \DateTime($data['created']);
68
69
            $comment = new Comment($data['id'], $post, $author, $toComment, $data['text'], $createdAt, $data['is_rec']);
1 ignored issue
show
Unused Code introduced by
The call to Comment::__construct() has too many arguments starting with $data['id'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
70
71
            $this->em->persist($comment);
72
        }
73
74
        return $comment;
75
    }
76
77
    /**
78
     * @param array $data
79
     *
80
     * @return Comment[]
81
     * @throws ApiException
82
     */
83
    public function createFromListArray(array $data)
84
    {
85
        $comments = [];
86
87
        foreach ($data as $commentData) {
88
            $comments[] = $this->createFromArray($commentData);
89
        }
90
91
        return $comments;
92
    }
93
94
    /**
95
     * @param array $data
96
     *
97
     * @throws InvalidResponseException
98
     */
99
    private function validateData(array $data)
100
    {
101
        if (!array_key_exists('author', $data)) {
102
            throw new InvalidResponseException('Comment author data not found in API response');
103
        }
104
105
        // Post
106
        if (!(
107
            array_key_exists('id', $data) &&
108
            array_key_exists('is_rec', $data) &&
109
            array_key_exists('to_comment_id', $data) &&
110
            array_key_exists('post_id', $data) &&
111
            array_key_exists('text', $data) &&
112
            array_key_exists('created', $data)
113
            )) {
114
            throw new InvalidResponseException('Comment data not found in API response');
115
        }
116
    }
117
}