Header::end()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace MyBuilder\Cronos\Formatter;
4
5
class Header
6
{
7
    /** @var Cron */
8
    private $cron;
9
10
    /** @var string */
11
    private $path;
12
13
    /** @var string */
14
    private $mailTo;
15
16
    /** @var string */
17
    private $home;
18
19
    /** @var string */
20
    private $shell;
21
22
    /** @var string */
23
    private $contentType;
24
25
    /** @var string */
26
    private $encoding;
27
28
    /** @var string */
29
    private $timezone;
30
31
    public function __construct(Cron $cron)
32
    {
33
        $this->cron = $cron;
34
    }
35
36
    /**
37
     * Works just like the shell PATH, but it does not inherit from your environment.
38
     */
39
    public function setPath(string $path): self
40
    {
41
        $this->path = $path;
42
43
        return $this;
44
    }
45
46
    public function setHome(string $home): self
47
    {
48
        $this->home = $home;
49
50
        return $this;
51
    }
52
53
    /** @throws InvalidEmail if given email is invalid */
54
    public function setMailto(string $email): self
55
    {
56
        $this->assertValidEmail($email);
57
        $this->mailTo = $email;
58
59
        return $this;
60
    }
61
62
    private function assertValidEmail($email): void
63
    {
64
        if (false === \filter_var($email, \FILTER_VALIDATE_EMAIL)) {
65
            throw new InvalidEmail($email);
66
        }
67
    }
68
69
    /**
70
     * Set the shell to be used when executing commands
71
     *
72
     * Default is /bin/sh but can also be changed to /bin/php
73
     */
74
    public function setShell(string $shell): self
75
    {
76
        $this->shell = $shell;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Set the content-type to use for cron output emails.
83
     */
84
    public function setContentType(string $contentType): self
85
    {
86
        $this->contentType = $contentType;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Set the charset to use for cron output emails.
93
     */
94
    public function setContentTransferEncoding(string $encoding): self
95
    {
96
        $this->encoding = $encoding;
97
98
        return $this;
99
    }
100
101
    public function setTimezone(string $timezone): self
102
    {
103
        $this->timezone = $timezone;
104
105
        return $this;
106
    }
107
108
    public function format(): string
109
    {
110
        $headers = '';
111
112
        if ($this->path) {
113
            $headers .= $this->createHeader('PATH', $this->path);
114
        }
115
        if ($this->mailTo) {
116
            $headers .= $this->createHeader('MAILTO', $this->mailTo);
117
        }
118
        if ($this->home) {
119
            $headers .= $this->createHeader('HOME', $this->home);
120
        }
121
        if ($this->shell) {
122
            $headers .= $this->createHeader('SHELL', $this->shell);
123
        }
124
        if ($this->contentType) {
125
            $headers .= $this->createHeader('CONTENT_TYPE', $this->contentType);
126
        }
127
        if ($this->encoding) {
128
            $headers .= $this->createHeader('CONTENT_TRANSFER_ENCODING', $this->encoding);
129
        }
130
        if ($this->timezone) {
131
            $headers .= $this->createHeader('CRON_TZ', $this->timezone);
132
        }
133
134
        return $headers;
135
    }
136
137
    private function createHeader($name, $value): string
138
    {
139
        return $name . '=' . $value . \PHP_EOL;
140
    }
141
142
    public function end(): Cron
143
    {
144
        return $this->cron;
145
    }
146
}
147