Payment::setLogger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * @author    Markus Tacker <[email protected]>
5
 * @copyright 2013-2016 Verein zur Förderung der Netzkultur im Rhein-Main-Gebiet e.V. | http://netzkultur-rheinmain.de/
6
 */
7
8
namespace BCRM\BackendBundle\Service;
9
10
use BCRM\BackendBundle\Event\Payment\PaymentVerifiedEvent;
11
use BCRM\BackendBundle\Service\Event\CreatePaymentCommand;
12
use BCRM\BackendBundle\Service\Payment\CheckPaymentCommand;
13
use LiteCQRS\Bus\CommandBus;
14
use LiteCQRS\Bus\EventMessageBus;
15
use LiteCQRS\Plugin\CRUD\Model\Commands\CreateResourceCommand;
16
use LiteCQRS\Plugin\CRUD\Model\Commands\UpdateResourceCommand;
17
use PhpOption\Option;
18
use Psr\Log\LoggerAwareInterface;
19
use Psr\Log\LoggerInterface;
20
use Doctrine\Common\Collections\ArrayCollection;
21
22
class Payment implements LoggerAwareInterface
23
{
24
    /**
25
     * @var \LiteCQRS\Bus\CommandBus
26
     */
27
    private $commandBus;
28
29
    /**
30
     * @var LoggerInterface
31
     */
32
    private $logger;
33
34
    /**
35
     * @var string
36
     */
37
    private $paypalIdentityToken;
38
39
    /**
40
     * @param CommandBus      $commandBus
41
     * @param EventMessageBus $eventMessageBus
42
     */
43
    public function __construct(CommandBus $commandBus, EventMessageBus $eventMessageBus, $paypalIdentityToken)
44
    {
45
        $this->commandBus          = $commandBus;
46
        $this->eventMessageBus     = $eventMessageBus;
0 ignored issues
show
Bug introduced by
The property eventMessageBus does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
        $this->paypalIdentityToken = $paypalIdentityToken;
48
    }
49
50
    /**
51
     * Sets a logger instance on the object
52
     *
53
     * @param LoggerInterface $logger
54
     *
55
     * @return null
56
     */
57
    public function setLogger(LoggerInterface $logger)
58
    {
59
        $this->logger = $logger;
60
    }
61
62
63
    public function createPayment(CreatePaymentCommand $command)
64
    {
65
        $createCommand        = new CreateResourceCommand();
66
        $createCommand->class = '\BCRM\BackendBundle\Entity\Payment';
67
        $createCommand->data  = array(
68
            'txId'    => $command->txId,
69
            'payload' => $command->payload,
70
            'method'  => $command->method
71
        );
72
        $this->commandBus->handle($createCommand);
73
    }
74
75
    public function checkPayment(CheckPaymentCommand $command)
76
    {
77
        // Verify transaction
78
        $payment = $command->payment;
79
80
        $url = $command->sandbox ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr';
81
        $url .= sprintf('?tx=%s&at=%s&cmd=_notify-synch', $payment->getTransactionId(), $this->paypalIdentityToken);
82
83
        $res = file_get_contents($url);
84
85
        $data = array();
86
        foreach (explode("\n", $res) as $line) {
87
            if (!strpos($line, '=')) continue;
88
            list($key, $value) = explode('=', $line);
89
            $data[$key] = $value;
90
        }
91
92
        $updateCommand        = new UpdateResourceCommand();
93
        $updateCommand->class = '\BCRM\BackendBundle\Entity\Payment';
94
        $updateCommand->id    = $payment->getId();
95
        $updateCommand->data  = array('checked' => new \DateTime());
96
97
        if (substr($res, 0, 7) === 'SUCCESS' && $data['payment_status'] === 'Completed') {
98
            $updateCommand->data['verified'] = true;
99
            $event                           = new PaymentVerifiedEvent();
100
            $event->payment                  = $payment;
101
            $payment->setPayload(new ArrayCollection($data));
102
            $this->eventMessageBus->publish($event);
103
            Option::fromValue($this->logger)->map(function (LoggerInterface $logger) use ($payment) {
104
                $logger->info(sprintf('Payment "%s" is verified.', $payment->getId()), array($payment));
105
            });
106 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
            Option::fromValue($this->logger)->map(function (LoggerInterface $logger) use ($payment) {
108
                $logger->alert(sprintf('Payment "%s" could not be verified.', $payment->getId()), array($payment));
109
            });
110
        }
111
112
        $this->commandBus->handle($updateCommand);
113
    }
114
}
115