Completed
Pull Request — master (#121)
by Dmitry
07:03 queued 01:13
created

NotPurchasableException::resolve()   A

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 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\cart;
12
13
use hiqdev\yii2\cart\ShoppingCart;
14
use yii\base\Exception;
15
16
/**
17
 * Interface PositionPurchasabilityValidatorInterface is thrown when cart
18
 * invariant validation restricts further purchase.
19
 */
20
abstract class NotPurchasableException extends Exception
21
{
22
    /** @var AbstractCartPosition|null */
23
    protected $position;
24
25
    /**
26
     * @var ShoppingCart
27
     */
28
    protected $cart;
29
30
    public static function forPosition(AbstractCartPosition $position, ShoppingCart $cart, string $message = '')
31
    {
32
        $exception = new static($message);
33
34
        $exception->position = $position;
35
        $exception->cart = $cart;
36
37
        return $exception;
38
    }
39
40
    public static function forCart(ShoppingCart $cart, string $message = '')
41
    {
42
        $exception = new static($message);
43
44
        $exception->cart = $cart;
45
46
        return $exception;
47
    }
48
49
    public function getName()
50
    {
51
        return 'Position is not purchasable';
52
    }
53
54
    /**
55
     * Method SHOULD BE called when exception is caught.
56
     * Child classes may override this method and add problem auto-resolving.
57
     *
58
     * @return bool whether exception was automatically resolved
59
     */
60
    public function resolve(): bool
61
    {
62
        return false;
63
    }
64
}
65