GenerateUrlCommand::getCodeToGeneratePayment()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 12
nop 2
1
<?php
2
3
namespace Alcalyn\PayplugBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Alcalyn\PayplugBundle\Model\Payment;
11
12
class GenerateUrlCommand extends ContainerAwareCommand
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('payplug:generate:url')
18
            ->setDescription('Generate a payment url')
19
            ->addOption('test', 't', InputOption::VALUE_NONE, 'Generate a test payment')
20
            ->addOption('code', null, InputOption::VALUE_NONE, 'Display code to generate this payment')
21
            ->addOption('interactive', 'i', InputOption::VALUE_NONE, 'Prompt every missed payment parameters')
22
            ->addArgument('amount', InputArgument::OPTIONAL, 'Amount of the payment in cents', 1600)
23
            ->addArgument('currency', InputArgument::OPTIONAL, 'Currency of the payment', Payment::EUROS)
24
            ->addOption('firstname', 'f', InputOption::VALUE_REQUIRED, 'First name of the customer')
25
            ->addOption('lastname', 'l', InputOption::VALUE_REQUIRED, 'Last name of the customer')
26
            ->addOption('email', null, InputOption::VALUE_REQUIRED, 'Email of the customer')
27
            ->addOption('ipn-url', null, InputOption::VALUE_REQUIRED, 'Ipn Url')
28
            ->addOption('return-url', null, InputOption::VALUE_REQUIRED, 'Return url')
29
            ->addOption('cancel-url', null, InputOption::VALUE_REQUIRED, 'Cancel url')
30
            ->addOption('customer', null, InputOption::VALUE_REQUIRED, 'Customer number')
31
            ->addOption('order', null, InputOption::VALUE_REQUIRED, 'Order number')
32
            ->addOption('custom-data', null, InputOption::VALUE_REQUIRED, 'Custom data')
33
            ->addOption('origin', null, InputOption::VALUE_REQUIRED, 'Origin of the payment')
34
        ;
35
    }
36
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $paymentService = $this->getContainer()->get('payplug.payment');
40
        $payment = $this->getPaymentFromInput($input);
41
        $test = $input->getOption('test');
42
        
43
        if ($input->getOption('interactive')) {
44
            $this->fillWithInteractiveMode($payment, $input, $output);
45
            $output->writeLn('');
46
        }
47
        
48
        if ($input->getOption('code')) {
49
            $output->writeLn($this->getCodeToGeneratePayment($payment, $test));
50
        } else {
51
            $output->writeLn($paymentService->generateUrl($payment, $test));
52
        }
53
    }
54
    
55
    /**
56
     * @param \Symfony\Component\Console\Input\InputInterface $input
57
     * 
58
     * @return Payment
59
     */
60
    private function getPaymentFromInput(InputInterface $input)
61
    {
62
        $payment = new Payment();
63
        
64
        return $payment
65
            ->setAmount($input->getArgument('amount'))
66
            ->setCurrency($input->getArgument('currency'))
67
            ->setFirstName($input->getOption('firstname'))
68
            ->setLastName($input->getOption('lastname'))
69
            ->setEmail($input->getOption('email'))
70
            ->setIpnUrl($input->getOption('ipn-url'))
71
            ->setReturnUrl($input->getOption('return-url'))
72
            ->setCancelUrl($input->getOption('cancel-url'))
73
            ->setCustomer($input->getOption('customer'))
74
            ->setOrder($input->getOption('order'))
75
            ->setCustomData($input->getOption('custom-data'))
76
            ->setOrigin($input->getOption('origin'))
77
        ;
78
    }
79
    
80
    /**
81
     * @param \Alcalyn\PayplugBundle\Model\Payment $payment
82
     * @param \Symfony\Component\Console\Input\InputInterface $input
83
     * @param \Symfony\Component\Console\Output\OutputInterface $output
84
     */
85
    private function fillWithInteractiveMode(Payment $payment, InputInterface $input, OutputInterface $output)
86
    {
87
        $dialog = $this->getHelperSet()->get('dialog');
88
        
89
        $output->writeLn('');
90
        
91
        $this->promptAmount($payment, $output, $dialog);
92
        $this->promptCurrency($payment, $output, $dialog);
93
        
94
        foreach (array('firstname', 'lastname', 'email') as $field) {
95
            $this->promptField($field, $payment, $input, $output, $dialog);
96
        }
97
        
98
        if (!$input->getOption('ipn-url')) {
99
            $defaultIpn = $this->getContainer()->get('payplug.payment')->getIpnUrl();
100
            
101
            $v = $dialog->ask($output, $this->promptFormat('IPN url ['.$defaultIpn.']'), $defaultIpn);
102
            $payment->setIpnUrl($v);
103
        }
104
        
105
        foreach (array('return-url', 'cancel-url', 'customer', 'order', 'custom-data', 'origin') as $field) {
106
            $this->promptField($field, $payment, $input, $output, $dialog);
107
        }
108
    }
109
    
110
    /**
111
     * @param \Alcalyn\PayplugBundle\Model\Payment $payment
112
     * @param \Symfony\Component\Console\Output\OutputInterface $output
113
     * @param mixed $dialog
114
     */
115
    private function promptAmount(Payment $payment, OutputInterface $output, $dialog)
116
    {
117
        $amount = $dialog->askAndValidate(
118
            $output,
119
            $this->promptFormat('Amount in cents'),
120
            function ($answer) {
121
                if (!preg_match('/[0-9]+/', $answer)) {
122
                    throw new \RunTimeException('Amount must be an integer');
123
                }
124
125
                return $answer;
126
            },
127
            false,
128
            1600
129
        );
130
131
        $payment->setAmount($amount);
132
    }
133
    
134
    /**
135
     * @param \Alcalyn\PayplugBundle\Model\Payment $payment
136
     * @param \Symfony\Component\Console\Output\OutputInterface $output
137
     * @param mixed $dialog
138
     */
139
    private function promptCurrency(Payment $payment, OutputInterface $output, $dialog)
140
    {
141
        $currency = $dialog->ask($output, $this->promptFormat('Currency [EUR]'), 'EUR');
142
        $payment->setCurrency($currency);
143
    }
144
    
145
    /**
146
     * @param string $field example: "custom-data"
147
     * @param \Alcalyn\PayplugBundle\Model\Payment $payment
148
     * @param \Symfony\Component\Console\Input\InputInterface $input
149
     * @param \Symfony\Component\Console\Output\OutputInterface $output
150
     * @param mixed $dialog
151
     */
152
    private function promptField($field, Payment $payment, InputInterface $input, OutputInterface $output, $dialog)
153
    {
154
        if (!$input->getOption($field)) {
155
            $v = $dialog->ask($output, $this->promptFormat(ucfirst(str_replace('-', ' ', $field))));
156
            $payment->{'set'.str_replace('-', '', $field)}($v);
157
        }
158
    }
159
    
160
    /**
161
     * @param string $s
162
     * @return string
163
     */
164
    private function promptFormat($s)
165
    {
166
        return str_pad($s.': ', 20, ' ', STR_PAD_LEFT);
167
    }
168
    
169
    /**
170
     * @param \Alcalyn\PayplugBundle\Model\Payment $payment
171
     * @param boolean $test whether it is a test payment
172
     * 
173
     * @return string
174
     */
175
    private function getCodeToGeneratePayment(Payment $payment, $test)
176
    {
177
        $code = <<<'CODE'
178
    $payment = new Payment();
179
180
    $payment
181
182
CODE;
183
        
184
        $code .= '        ->setAmount('.$payment->getAmount().')'.PHP_EOL;
185
        
186
        $attributes = array(
187
            'Currency',     'FirstName',    'LastName', 'Email',    'ReturnUrl',
188
            'CancelUrl',    'Customer',     'Customer', 'Order',    'CustomData',   'Origin',
189
        );
190
        
191
        foreach ($attributes as $attribute) {
192
            if (null !== $payment->{'get'.$attribute}()) {
193
                $code .= '        ->set'.$attribute.'(\''.$payment->{'get'.$attribute}().'\')'.PHP_EOL;
194
            }
195
        }
196
        
197
        $code .= '    ;'.PHP_EOL.PHP_EOL;
198
        $code .= '    // Get Payplug '.($test ? 'test ' : '').'payment service'.PHP_EOL;
199
        $code .= '    $payplugPayment = $this->get(\'payplug.payment'.($test ? '.test' : '').'\');'.PHP_EOL.PHP_EOL;
200
        $code .= '    // Generate payment url'.PHP_EOL;
201
        $code .= '    $paymentUrl = $payplugPayment->generateUrl($payment);'.PHP_EOL;
202
        
203
        return $code;
204
    }
205
}
206