PathPrefixCondition::evaluate()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Middleware
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Middleware\Condition;
16
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Phossa2\Shared\Base\ObjectAbstract;
20
use Phossa2\Middleware\Interfaces\ConditionInterface;
21
22
/**
23
 * PathPrefixCondition
24
 *
25
 * Modified from https://github.com/woohoolabs/harmony
26
 *
27
 * @package Phossa2\Middleware
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     ObjectAbstract
30
 * @see     ConditionInterface
31
 * @version 2.0.0
32
 * @since   2.0.0 added
33
 */
34
class PathPrefixCondition extends ObjectAbstract implements ConditionInterface
35
{
36
    /**
37
     * @var    string[]
38
     * @access protected
39
     */
40
    protected $prefix;
41
42
    /**
43
     * @param  string|string[] $pathPrefix
44
     * @access public
45
     */
46
    public function __construct($pathPrefix)
47
    {
48
        $this->prefix = (array) $pathPrefix;
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function evaluate(
55
        RequestInterface $request,
56
        ResponseInterface $response
57
    )/*# : bool */ {
58
        foreach ($this->prefix as $pref) {
59
            if (0 === strpos($request->getUri()->getPath(), $pref)) {
60
                return true;
61
            }
62
        }
63
        return false;
64
    }
65
}
66