Completed
Push — master ( 9aa90f...051175 )
by Paul
06:58
created

IssueToEntityTransformer::transform()   B

Complexity

Conditions 10
Paths 17

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 22
c 0
b 0
f 0
nc 17
nop 1
dl 0
loc 35
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of the GitlabIssueBundle package.
4
 *
5
 * (c) Paul Schweppe <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace VersionControl\GitlabIssueBundle\DataTransformer;
12
13
use VersionControl\GitlabIssueBundle\Entity\Issues\Issue;
14
use Symfony\Component\Form\Exception\TransformationFailedException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Form\E...ormationFailedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
class IssueToEntityTransformer implements DataTransformerInterface
17
{
18
    private $issueLabelTransformer;
19
20
    private $issueMilestoneTransformer;
21
22
    private $issueCommentTransformer;
23
24
    private $userTransformer;
25
26
    public function __construct()
27
    {
28
        $this->issueLabelTransformer = new IssueLabelToEntityTransformer();
29
        $this->issueMilestoneTransformer = new IssueMilestoneToEntityTransformer();
30
        $this->issueCommentTransformer = new IssueCommentToEntityTransformer();
31
        $this->userTransformer = new UserToEntityTransformer();
32
    }
33
34
    /**
35
     * Transforms an issue array into an issue Entity object.
36
     *
37
     * @param Issue|null $issue
38
     *
39
     * @return string
40
     */
41
    public function transform($issue)
42
    {
43
        if (null === $issue) {
44
            return null;
45
        }
46
47
        $issueEntity = new Issue();
48
        $issueEntity->setId($issue['id']);
49
        $issueEntity->setTitle($issue['title']);
50
        $issueEntity->setStatus($this->formatState($issue['state']));
51
        $issueEntity->setDescription($issue['description']);
52
        $issueEntity->setCreatedAt($this->formatDate($issue['created_at']));
53
        if (isset($issue['closed_at'])) {
54
            $issueEntity->setClosedAt($this->formatDate($issue['closed_at']));
55
        }
56
        $issueEntity->setUpdatedAt($this->formatDate($issue['updated_at']));
57
58
        //Map Issue labels
59
        if (isset($issue['labels']) && is_array($issue['labels'])) {
60
            foreach ($issue['labels'] as $label) {
61
                $issueLabelEntity = $this->issueLabelTransformer->transform($label);
62
                $issueEntity->addIssueLabel($issueLabelEntity);
0 ignored issues
show
Bug introduced by
$issueLabelEntity of type string is incompatible with the type VersionControl\GitlabIss...ntity\Issues\IssueLabel expected by parameter $issueLabel of VersionControl\GitlabIss...\Issue::addIssueLabel(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
                $issueEntity->addIssueLabel(/** @scrutinizer ignore-type */ $issueLabelEntity);
Loading history...
63
            }
64
        }
65
66
        if (isset($issue['author']) && is_array($issue['author'])) {
67
            $user = $this->userTransformer->transform($issue['author']);
68
            $issueEntity->setUser($user);
0 ignored issues
show
Bug introduced by
$user of type string is incompatible with the type VersionControl\GitlabIssueBundle\Entity\User|null expected by parameter $user of VersionControl\GitlabIss...Issues\Issue::setUser(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
            $issueEntity->setUser(/** @scrutinizer ignore-type */ $user);
Loading history...
69
        }
70
        if (isset($issue['milestone']) && is_array($issue['milestone'])) {
71
            $issueMilestoneEntity = $this->issueMilestoneTransformer->transform($issue['milestone']);
72
            $issueEntity->setIssueMilestone($issueMilestoneEntity);
0 ignored issues
show
Bug introduced by
$issueMilestoneEntity of type string is incompatible with the type VersionControl\GitlabIss...ues\IssueMilestone|null expected by parameter $issueMilestone of VersionControl\GitlabIss...ue::setIssueMilestone(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
            $issueEntity->setIssueMilestone(/** @scrutinizer ignore-type */ $issueMilestoneEntity);
Loading history...
73
        }
74
75
        return $issueEntity;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $issueEntity returns the type VersionControl\GitlabIss...dle\Entity\Issues\Issue which is incompatible with the documented return type string.
Loading history...
76
    }
77
78
    /**
79
     * Transforms an issue entity into a git api captiable issue array.
80
     *
81
     * @param \VersionControl\GitlabIssueBundle\Entity\Issues $issueEntity
82
     *
83
     * @return array|null
84
     *
85
     * @throws TransformationFailedException if object (issue) is not found
86
     */
87
    public function reverseTransform($issueEntity)
88
    {
89
        if ($issueEntity === null) {
90
            // causes a validation error
91
            throw new TransformationFailedException('IssueEntity is null');
92
        }
93
94
        $issue = array(
95
            'title' => $issueEntity->getTitle(), 'description' => $issueEntity->getDescription(),
96
            //,'state' =>  $issueEntity->getStatus()
97
            //,'milestone' =>  0
98
        );
99
        if ($issueEntity->getIssueMilestone()) {
100
            $issue['milestone_id'] = $issueEntity->getIssueMilestone()->getId();
101
        }
102
        $labels = array();
103
        foreach ($issueEntity->getIssueLabel() as $issueLabel) {
104
            $labels[] = $issueLabel->getId();
105
        }
106
        $issue['labels'] = implode(',', $labels);
107
108
        return $issue;
109
    }
110
111
    protected function formatDate($date)
112
    {
113
        try {
114
            $dateTime = new \DateTime($date);
115
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type VersionControl\GitlabIss...taTransformer\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
116
            echo $e->getMessage();
117
        }
118
119
        return $dateTime;
120
    }
121
122
    protected function formatState($state)
123
    {
124
        if ($state === 'opened' || $state === 'activated') {
125
            return 'open';
126
        }
127
128
        return $state;
129
    }
130
}
131