IssueToEntityTransformer::transform()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 33
rs 8.0555
1
<?php
2
/*
3
 * This file is part of the GithubIssueBundle 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\GithubIssueBundle\DataTransformer;
12
13
use VersionControl\GithubIssueBundle\Entity\Issues\Issue;
14
use Symfony\Component\Form\Exception\TransformationFailedException;
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['number']);
49
        $issueEntity->setTitle($issue['title']);
50
        $issueEntity->setStatus($issue['state']);
51
        $issueEntity->setDescription($issue['body']);
52
        $issueEntity->setCreatedAt($this->formatDate($issue['created_at']));
53
        $issueEntity->setClosedAt($this->formatDate($issue['closed_at']));
54
        $issueEntity->setUpdatedAt($this->formatDate($issue['updated_at']));
55
56
        //Map Issue labels
57
        if (isset($issue['labels']) && is_array($issue['labels'])) {
58
            foreach ($issue['labels'] as $label) {
59
                $issueLabelEntity = $this->issueLabelTransformer->transform($label);
60
                $issueEntity->addIssueLabel($issueLabelEntity);
0 ignored issues
show
Bug introduced by
$issueLabelEntity of type string is incompatible with the type VersionControl\GithubIss...ntity\Issues\IssueLabel expected by parameter $issueLabel of VersionControl\GithubIss...\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

60
                $issueEntity->addIssueLabel(/** @scrutinizer ignore-type */ $issueLabelEntity);
Loading history...
61
            }
62
        }
63
64
        if (isset($issue['user']) && is_array($issue['user'])) {
65
            $user = $this->userTransformer->transform($issue['user']);
66
            $issueEntity->setUser($user);
0 ignored issues
show
Bug introduced by
$user of type string is incompatible with the type VersionControl\GithubIssueBundle\Entity\User|null expected by parameter $user of VersionControl\GithubIss...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

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

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