PdoCouponSheetContainer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 46
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A has() 0 10 2
A get() 0 12 3
1
<?php
2
namespace Germania\Coupons;
3
4
use Psr\Container\ContainerInterface;
5
use Psr\Container\NotFoundException;
6
7
class PdoCouponSheetContainer implements ContainerInterface
8
{
9
10
    public $instances = array();
11
    /**
12
     * @var callable
13
     */
14
    public $sheet_factory;
15
16
    public function __construct( callable $sheet_factory )
17
    {
18
        $this->sheet_factory = $sheet_factory;
19
    }
20
21
    /**
22
     * @param  string|int $id
23
     */
24
    public function has( $id )
25
    {
26
        if (array_key_exists( $id, $this->instances))
27
            return !empty( $this->instances[ $id ] );
28
29
        $sheet_factory = $this->sheet_factory;
30
31
        $this->instances[ $id ] = $sheet_factory( $id );
32
        return !empty( $this->instances[ $id ] );
33
    }
34
35
36
    /**
37
     * @param  string|int $id
38
     */
39
    public function get( $id )
40
    {
41
        if (!empty( $this->instances[ $id ]))
42
            return $this->instances[ $id ];
43
44
        if (!$instance = $sheet_factory( $id ))
0 ignored issues
show
Bug introduced by
The variable $sheet_factory does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
45
            throw new CouponSheetNotFoundException("Could not find a coupon sheet with ID $id");
46
47
48
        $this->instances[ $id ] = $instance;
49
        return $instance;
50
    }
51
52
}
53
54