Passed
Push — trunk ( f4162f...7ce20e )
by Christian
12:30 queued 14s
created

PromotionException::patternAlreadyInUse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Promotion;
4
5
use Shopware\Core\Checkout\Promotion\Exception\PatternAlreadyInUseException;
6
use Shopware\Core\Checkout\Promotion\Exception\PatternNotComplexEnoughException;
7
use Shopware\Core\Framework\Feature;
8
use Shopware\Core\Framework\HttpException;
9
use Shopware\Core\Framework\Log\Package;
10
use Symfony\Component\HttpFoundation\Response;
11
12
#[Package('checkout')]
13
class PromotionException extends HttpException
14
{
15
    public const PROMOTION_CODE_ALREADY_REDEEMED = 'CHECKOUT__CODE_ALREADY_REDEEMED';
16
17
    public const INVALID_CODE_PATTERN = 'CHECKOUT__INVALID_CODE_PATTERN';
18
19
    public const PATTERN_NOT_COMPLEX_ENOUGH = 'PROMOTION__INDIVIDUAL_CODES_PATTERN_INSUFFICIENTLY_COMPLEX';
20
21
    public const PATTERN_ALREADY_IN_USE = 'PROMOTION__INDIVIDUAL_CODES_PATTERN_ALREADY_IN_USE';
22
23
    public static function codeAlreadyRedeemed(string $code): self
24
    {
25
        return new self(
26
            Response::HTTP_BAD_REQUEST,
27
            self::PROMOTION_CODE_ALREADY_REDEEMED,
28
            'Promotion with code "{{ code }}" has already been marked as redeemed!',
29
            ['code' => $code]
30
        );
31
    }
32
33
    public static function invalidCodePattern(string $codePattern): self
34
    {
35
        return new self(
36
            Response::HTTP_BAD_REQUEST,
37
            self::INVALID_CODE_PATTERN,
38
            'Invalid code pattern "{{ codePattern }}".',
39
            ['codePattern' => $codePattern]
40
        );
41
    }
42
43
    public static function patternNotComplexEnough(): self
44
    {
45
        if (!Feature::isActive('v6.6.0.0')) {
46
            return new PatternNotComplexEnoughException();
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\Checkout\P...tComplexEnoughException has been deprecated: tag:v6.6.0 - will be removed, use PromotionException::patternNotComplexEnough instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

46
            return /** @scrutinizer ignore-deprecated */ new PatternNotComplexEnoughException();
Loading history...
47
        }
48
49
        return new self(
50
            Response::HTTP_BAD_REQUEST,
51
            self::PATTERN_NOT_COMPLEX_ENOUGH,
52
            'The amount of possible codes is too low for the current pattern. Make sure your pattern is sufficiently complex.'
53
        );
54
    }
55
56
    public static function patternAlreadyInUse(): self
57
    {
58
        if (!Feature::isActive('v6.6.0.0')) {
59
            return new PatternAlreadyInUseException();
0 ignored issues
show
Deprecated Code introduced by
The class Shopware\Core\Checkout\P...rnAlreadyInUseException has been deprecated: tag:v6.6.0 - will be removed, use PromotionException::patternAlreadyInUse instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

59
            return /** @scrutinizer ignore-deprecated */ new PatternAlreadyInUseException();
Loading history...
60
        }
61
62
        return new self(
63
            Response::HTTP_BAD_REQUEST,
64
            self::PATTERN_ALREADY_IN_USE,
65
            'Code pattern already exists in another promotion. Please provide a different pattern.'
66
        );
67
    }
68
}
69