Test Failed
Branch feature/refactoring-samurai (e4c420)
by Giuliano
05:24
created

Author::getDist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Maestriam\Samurai\Entities;
4
5
use Maestriam\Samurai\Exceptions\InvalidAuthorException;
6
7
class Author extends Foundation 
8
{
9
    /**
10
     * Nome do autor do tema
11
     * 
12
     * @var string
13
     */
14
    private string $name; 
15
    
16
    /**
17
     * E-mail do autor do tema
18
     * 
19
     * @var string
20
     */
21
    private string $email;
22
23
    /**
24
     * Distribuidora do autor
25
     */
26
    private string $dist;
0 ignored issues
show
introduced by
The private property $dist is not used, and could be removed.
Loading history...
27
28
    /**
29
     * Instância com as regras de negócios sobre o autor do tema.  
30
     * Na ausência de informações sobre o autor, irá pegar as informações
31
     * definida nas configurações do projeto como padrão.  
32
     *
33
     * @param string $author
34
     */
35
    public function __construct(string $author = null)
36
    {
37
        ($author) ? $this->set($author) : $this->default();
38
    }    
39
40
    /**
41
     * Retorna/Define o nome do autor do tema.  
42
     * Se passar uma string como parâmetro, assume a função de definição.  
43
     * Informações utilizados dentro arquivo composer.json.  
44
     *
45
     * @param string $name
46
     * @return Author|string
47
     */
48
    public function name(string $name = null) : Author|string
49
    {
50
        if (! $name)  {
51
            return $this->getName();
52
        }  
53
        
54
        return $this->setName($name);
55
    }
56
57
    /**
58
     * Retorna/Define o e-mail do autor do tema.  
59
     * Se passar uma string como parâmetro, assume a função de definição.  
60
     * Informações utilizados dentro arquivo composer.json  
61
     *
62
     * @param string $email
63
     * @return Author|string
64
     */
65
    public function email(string $email = null) : Author|string
66
    {
67
        if (! $email) {
68
            return $this->getEmail();
69
        } 
70
        
71
        return $this->setEmail($email);
72
    }
73
74
    /**
75
     * Retorna a assinatura do autor.   
76
     * Ex: Joe Doe <[email protected]>
77
     *
78
     * @return string
79
     */
80
    public function signature() : string
81
    {
82
        return sprintf("%s <%s>", $this->name(), $this->email());
83
    }
84
85
    /**
86
     * Define o nome e o autor do tema, de acordo com o padrão:  
87
     * Ex: Giu Sampaio <<[email protected]>>
88
     * 
89
     * @param string $author
90
     * @return Author
91
     * @throws InvalidAuthorException
92
     */
93
    public function set(string $author) : Author
94
    {
95
        if (! $this->valid()->author($author)) {
96
            throw new InvalidAuthorException($author);
97
        }
98
        
99
        $author = $this->parser()->author($author);       
100
101
        return $this->load($author);
102
    }
103
104
    /**
105
     * Carrega o nome, distribuidora e o e-mail do autor de um objeto específico
106
     *
107
     * @param object $author
108
     * @return Author
109
     */
110
    private function load(object $author) : Author
111
    {
112
        return $this->name($author->name)->email($author->email);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->name($auth...->email($author->email) could return the type string which is incompatible with the type-hinted return Maestriam\Samurai\Entities\Author. Consider adding an additional type-check to rule them out.
Loading history...
113
    }    
114
115
    /**
116
     * Define o nome do autor do tema
117
     * Usado no arquivo composer.json
118
     *
119
     * @param string $name
120
     * @return Author
121
     */
122
    private function setName(string $name) : Author
123
    {
124
        $this->name = $name;
125
        return $this;
126
    }
127
128
    /**
129
     * Retorna o nome do autor do tema
130
     *
131
     * @return string
132
     */
133
    private function getName() : string
134
    {
135
        return $this->name ?? $this->config()->author()->name;
136
    }    
137
    
138
    /**
139
     * Define o email do autor do tema
140
     *
141
     * @param string $email
142
     * @return Author
143
     */
144
    private function setEmail(string $email) : Author
145
    {
146
        $this->email = $email;
147
        return $this;
148
    }
149
150
    /**
151
     * Retorna o e-mail do autor do tema
152
     *
153
     * @return string
154
     */
155
    private function getEmail() : string
156
    {
157
        return $this->email ?? $this->config()->author()->email;
158
    }
159
160
    /**
161
     * Retorna as informações padrões do autor, definido no config
162
     *
163
     * @return Author
164
     */
165
    private function default() : Author
166
    {
167
        $default = $this->config()->author();
168
        
169
        return $this->load($default);    
170
    }
171
}