Completed
Push — master ( 42105c...ac8bed )
by Lucas Pires
02:12
created

Issue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace FlyingLuscas\Laker;
4
5
use Exception;
6
use ReflectionClass;
7
use FlyingLuscas\Laker\Contracts\ServiceContract;
8
9
class Issue
10
{
11
    /**
12
     * The title of the issue.
13
     *
14
     * @var string
15
     */
16
    protected $title;
17
18
    /**
19
     * The body of the issue.
20
     *
21
     * @var string
22
     */
23
    protected $body;
24
25
    /**
26
     * Create a new class instance.
27
     *
28
     * @param Exception $e
29
     */
30
    public function __construct(Exception $e)
31
    {
32
        $this->setTitle($this->getIssueTitleFromException($e));
33
        $this->setBody($e->getMessage());
34
    }
35
36
    /**
37
     * Create issue on the given service.
38
     *
39
     * @param  \FlyingLuscas\Laker\Contracts\ServiceContract $service
40
     *
41
     * @return void
42
     */
43
    public function createOn(ServiceContract $service)
44
    {
45
        $service->createIssue($this);
46
    }
47
48
    /**
49
     * Get the title of the issue from an exception.
50
     *
51
     * @param Exception $e
52
     *
53
     * @return string
54
     */
55
    private function getIssueTitleFromException(Exception $e)
56
    {
57
        $reflection = new ReflectionClass($e);
58
59
        return sprintf('%s in %s line %d', $reflection->getShortName(), basename($e->getFile()), $e->getLine());
60
    }
61
62
    /**
63
     * Set the title of the issue.
64
     *
65
     * @param string $title
66
     *
67
     * @return self
68
     */
69
    public function setTitle($title)
70
    {
71
        $this->title = $title;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Set the body of the issue.
78
     *
79
     * @param string $body
80
     *
81
     * @return self
82
     */
83
    public function setBody($body)
84
    {
85
        $this->body = $body;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Get the issue title.
92
     *
93
     * @return string
94
     */
95
    public function getTitle()
96
    {
97
        return $this->title;
98
    }
99
100
    /**
101
     * Get the body of the issue.
102
     *
103
     * @return string
104
     */
105
    public function getBody()
106
    {
107
        return $this->body;
108
    }
109
}
110