PdoCouponSheetContainer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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