Completed
Push — master ( ce2be0...7bcb9a )
by Olivier
02:08
created

CouponCreate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 69
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getConfigTreeBuilder() 0 66 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Shapin\Stripe\Configuration;
11
12
use Shapin\Stripe\Model\Coupon\Coupon;
13
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
14
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
16
class CouponCreate implements ConfigurationInterface
17
{
18 3
    public function getConfigTreeBuilder()
19
    {
20 3
        $treeBuilder = new TreeBuilder('shapin_stripe_coupon_create');
21 3
        $rootNode = $treeBuilder->getRootNode();
22
23
        $rootNode
24 3
            ->children()
25 3
                ->scalarNode('id')
26 3
                    ->info('Unique string of your choice that will be used to identify this coupon when applying it to a customer. This is often a specific code you’ll give to your customer to use when signing up (e.g., FALL25OFF). If you don’t want to specify a particular code, you can leave the ID blank and we’ll generate a random code for you.')
27 3
                ->end()
28 3
                ->enumNode('duration')
29 3
                    ->isRequired()
30 3
                    ->values([Coupon::DURATION_FOREVER, Coupon::DURATION_ONCE, Coupon::DURATION_REPEATING])
31 3
                    ->info('Specifies how long the discount will be in effect. Can be forever, once, or repeating.')
32 3
                ->end()
33 3
                ->integerNode('amount_off')
34 3
                    ->info('A positive integer representing the amount to subtract from an invoice total (required if percent_off is not passed).')
35 3
                ->end()
36 3
                ->scalarNode('currency')
37 3
                    ->info('Three-letter ISO code for the currency of the amount_off parameter (required if amount_off is passed).')
38 3
                ->end()
39 3
                ->integerNode('duration_in_months')
40 3
                    ->info('Required only if duration is repeating, in which case it must be a positive integer that specifies the number of months the discount will be in effect.')
41 3
                ->end()
42 3
                ->integerNode('max_redemptions')
43 3
                    ->info('A positive integer specifying the number of times the coupon can be redeemed before it’s no longer valid. For example, you might have a 50% off coupon that the first 20 readers of your blog can use.')
44 3
                ->end()
45 3
                ->arrayNode('metadata')
46 3
                    ->scalarPrototype()->end()
47 3
                    ->info('Set of key-value pairs that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to metadata.')
48 3
                ->end()
49 3
                ->scalarNode('name')
50 3
                    ->info('Name of the coupon displayed to customers on, for instance invoices, or receipts. By default the id is shown if name is not set.')
51 3
                ->end()
52 3
                ->floatNode('percent_off')
53 3
                    ->info('A positive float larger than 0, and smaller or equal to 100, that represents the discount the coupon will apply (required if amount_off is not passed).')
54 3
                ->end()
55 3
                ->integerNode('redeem_by')
56 3
                    ->info('Unix timestamp specifying the last time at which the coupon can be redeemed. After the redeem_by date, the coupon can no longer be applied to new customers.')
57 3
                ->end()
58 3
            ->end()
59
        ;
60
61
        $rootNode
62 3
            ->validate()
63
                ->ifTrue(function ($v) {
64 3
                    return Coupon::DURATION_REPEATING === $v['duration'] && !isset($v['duration_in_months']);
65 3
                })
66 3
                ->thenInvalid('"duration_in_months" is required when using  "repeating" duration.')
67 3
            ->end()
68 3
            ->validate()
69
                ->ifTrue(function ($v) {
70 2
                    return isset($v['amount_off']) && !isset($v['currency']);
71 3
                })
72 3
                ->thenInvalid('"currency" is required when specifying  "amount_off".')
73 3
            ->end()
74 3
            ->validate()
75
                ->ifTrue(function ($v) {
76 1
                    return !isset($v['amount_off']) && !isset($v['percent_off']);
77 3
                })
78 3
                ->thenInvalid('You must specify either an "amount_off" or an "percent_off".')
79 3
            ->end()
80
        ;
81
82 3
        return $treeBuilder;
83
    }
84
}
85