Completed
Push — master ( c86066...40a59e )
by Lucas Pires
02:03
created

Issue::getIssueBodyFromException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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($this->getIssueBodyFromException($e));
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 body of the issue from an exception.
50
     *
51
     * @param Exception $e
52
     *
53
     * @return string
54
     */
55
    private function getIssueBodyFromException(Exception $e)
56
    {
57
        return sprintf("%s in %s line %d\n\n%s", get_class($e), $e->getFile(), $e->getLine(), $e->getMessage());
58
    }
59
60
    /**
61
     * Get the title of the issue from an exception.
62
     *
63
     * @param Exception $e
64
     *
65
     * @return string
66
     */
67
    private function getIssueTitleFromException(Exception $e)
68
    {
69
        $reflection = new ReflectionClass($e);
70
71
        return sprintf('%s in %s line %d', $reflection->getShortName(), basename($e->getFile()), $e->getLine());
72
    }
73
74
    /**
75
     * Set the title of the issue.
76
     *
77
     * @param string $title
78
     *
79
     * @return self
80
     */
81
    public function setTitle($title)
82
    {
83
        $this->title = $title;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Set the body of the issue.
90
     *
91
     * @param string $body
92
     *
93
     * @return self
94
     */
95
    public function setBody($body)
96
    {
97
        $this->body = $body;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Get the issue title.
104
     *
105
     * @return string
106
     */
107
    public function getTitle()
108
    {
109
        return $this->title;
110
    }
111
112
    /**
113
     * Get the body of the issue.
114
     *
115
     * @return string
116
     */
117
    public function getBody()
118
    {
119
        return $this->body;
120
    }
121
}
122