TinyMailListBuilder::storeEmail()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
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