Completed
Push — develop ( 113bf8...019067 )
by Neomerx
05:45 queued 04:14
created

HasMiddlewareTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 40
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setMiddleware() 0 17 3
A addMiddleware() 0 4 1
1
<?php namespace Limoncello\Core\Routing\Traits;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Closure;
20
use LogicException;
21
use Psr\Container\ContainerInterface;
22
use Psr\Http\Message\ResponseInterface;
23
use Psr\Http\Message\ServerRequestInterface;
24
25
/**
26
 * @package Limoncello\Core
27
 *
28
 * @method string getCallableToCacheMessage();
29
 */
30
trait HasMiddlewareTrait
31
{
32
    /**
33
     * @var callable[]
34
     */
35
    private $middleware = [];
36
37
    /**
38
     * @param callable[] $middleware
39
     *
40
     * @return self
41
     */
42 20
    public function setMiddleware(array $middleware): self
43
    {
44 20
        foreach ($middleware as $item) {
45 16
            $isValid = $this->checkPublicStaticCallable($item, [
0 ignored issues
show
Bug introduced by
It seems like checkPublicStaticCallable() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
46 16
                ServerRequestInterface::class,
47
                Closure::class,
48
                ContainerInterface::class,
49 16
            ], ResponseInterface::class);
50 16
            if ($isValid === false) {
51 16
                throw new LogicException($this->getCallableToCacheMessage());
52
            }
53
        }
54
55 18
        $this->middleware = $middleware;
56
57 18
        return $this;
58
    }
59
60
    /**
61
     * @param callable[] $middleware
62
     *
63
     * @return self
64
     */
65
    public function addMiddleware(array $middleware): self
66
    {
67
        return $this->setMiddleware(array_merge($this->middleware, $middleware));
68
    }
69
}
70