TinyMailListBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace PiedWeb\TinyMailListBuilder;
4
5
class TinyMailListBuilder
6
{
7
    protected $listFolder;
8
    protected $mailList;
9
10 3
    public function __construct($mailList = [], $listFolder = 'list')
11
    {
12 3
        $this->listFolder = $listFolder;
13 3
        $this->mailList = $mailList;
14 3
    }
15
16 3
    public function add($email, $list)
17
    {
18 3
        $email = $this->checkEmail($email);
19 3
        if ($email) {
20 3
            return $this->storeEmail($email, $list);
21
        }
22
    }
23
24 3
    protected function checkEmail($email)
25
    {
26 3
        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
27 3
            return $email;
28
        }
29
30
        return false;
31
    }
32
33 3
    protected function storeEmail($email, $list)
34
    {
35 3
        $list = in_array($list, $this->mailList) ? $list : 'default';
36 3
        $file = $this->listFolder.'/'.$list.'.txt';
37 3
        if (!exec('grep '.escapeshellarg($email).' '.$file)) {
38 3
            return file_put_contents($file, $email.PHP_EOL, FILE_APPEND);
39
        }
40
    }
41
}
42