Completed
Push — develop ( 10d90b...60de6f )
by greg
10:54
created

MailDomain::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
//filename : module/TutorialValidator/src/TutorialValidator/Validator/Special.php
3
namespace PlaygroundCore\Validator;
4
5
use Zend\Validator\AbstractValidator;
6
use Zend\Validator\Exception;
7
8
class MailDomain extends AbstractValidator
9
{
10
    const FORBIDDEN = 'FORBIDDEN';
11
12
    protected $options = array(
13
        'file' => null,  // File containing the authorized domains
14
    );
15
 
16
    protected $messageTemplates = array(
17
        self::FORBIDDEN => "This domain is not allowed",
18
    );
19
     
20
    public function __construct($options = null)
21
    {
22
        if (is_string($options)) {
23
            $this->options = array('file' => str_replace('\\', '/', getcwd()) . '/' . ltrim($options, '/'));
24
        } elseif (is_array($options)) {
25
            $this->options = array('file' => str_replace('\\', '/', getcwd()) . '/' . ltrim(reset($options), '/'));
26
        }
27
28
        parent::__construct($this->options);
29
    }
30
31
    /**
32
     * Returns the file path
33
     *
34
     * @return int
35
     */
36
    public function getFile()
37
    {
38
        return $this->options['file'];
39
    }
40
41
    /**
42
     * Sets the path to the file
43
     *
44
     * @param  string $path to the file
0 ignored issues
show
Bug introduced by
There is no parameter named $path. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
45
     * @return MailDomain Provides a fluent interface
46
     * @throws Exception\InvalidArgumentException When file is not found
47
     */
48 View Code Duplication
    public function setFile($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        if (empty($file) || false === stream_resolve_include_path($file)) {
51
            throw new Exception\InvalidArgumentException('Invalid options to validator provided');
52
        }
53
54
        $this->options['file'] = $file;
55
        return $this;
56
    }
57
 
58
    public function isValid($value)
59
    {
60
        // get only the mail domain
61
        $domain = explode('@', $value);
62
        $domain = end($domain);
63
        $this->setValue(strtolower($value));
64
65
        if (strpos(file_get_contents($this->getFile()), strtolower($domain)) === false) {
66
            $this->error(self::FORBIDDEN);
67
68
            return false;
69
        } 
70
71
        return true;
72
    }
73
}
74