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

Issue   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 113
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createOn() 0 4 1
A getIssueBodyFromException() 0 4 1
A getIssueTitleFromException() 0 6 1
A setTitle() 0 6 1
A setBody() 0 6 1
A getTitle() 0 4 1
A getBody() 0 4 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