Completed
Push — master ( 127834...00088d )
by Andrii
13:27 queued 15s
created

NotPurchasableException::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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