NotPurchasableException   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 45
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A forPosition() 0 9 1
A forCart() 0 8 1
A getName() 0 4 1
A resolve() 0 4 1
1
<?php
2
3
namespace hiqdev\yii2\cart;
4
5
use yii\base\Exception;
6
7
/**
8
 * Interface PositionPurchasabilityValidatorInterface is thrown when cart
9
 * invariant validation restricts further purchase.
10
 */
11
abstract class NotPurchasableException extends Exception
12
{
13
    /** @var CartPositionInterface|null */
14
    protected $position;
15
16
    /**
17
     * @var ShoppingCart|null
18
     */
19
    protected $cart;
20
21
    public static function forPosition(CartPositionInterface $position, ShoppingCart $cart, string $message = '')
22
    {
23
        $exception = new static($message);
24
25
        $exception->position = $position;
26
        $exception->cart = $cart;
27
28
        return $exception;
29
    }
30
31
    public static function forCart(ShoppingCart $cart, string $message = '')
32
    {
33
        $exception = new static($message);
34
35
        $exception->cart = $cart;
36
37
        return $exception;
38
    }
39
40
    public function getName()
41
    {
42
        return 'Position is not purchasable';
43
    }
44
45
    /**
46
     * Method SHOULD BE called when exception is caught.
47
     * Child classes may override this method and add problem auto-resolving.
48
     *
49
     * @return bool whether exception was automatically resolved
50
     */
51
    public function resolve(): bool
52
    {
53
        return false;
54
    }
55
}
56