Completed
Push — master ( 0d9cfe...073f2f )
by Joachim
12:30
created

CallbackManager::getClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace Loevgaard\DandomainAltapayBundle\Manager;
3
4
use Doctrine\Common\Persistence\ObjectManager;
5
use Doctrine\Common\Persistence\ObjectRepository;
6
use Loevgaard\DandomainAltapayBundle\Entity\CallbackInterface;
7
use Loevgaard\DandomainAltapayBundle\Entity\OrderLineInterface;
8
use Loevgaard\DandomainAltapayBundle\Entity\TerminalInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
11
class CallbackManager
12
{
13
    /**
14
     * @var ObjectManager
15
     */
16
    protected $objectManager;
17
18
    /**
19
     * @var string
20
     */
21
    protected $class;
22
23
    public function __construct(ObjectManager $objectManager, string $class)
24
    {
25
        $this->objectManager = $objectManager;
26
        $this->class = $class;
27
    }
28
29
    /**
30
     * @return ObjectRepository
31
     */
32
    protected function getRepository() : ObjectRepository
33
    {
34
        return $this->objectManager->getRepository($this->getClass());
35
    }
36
37
    /**
38
     * @return string
39
     */
40 View Code Duplication
    public function getClass() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
41
    {
42
        if (false !== strpos($this->class, ':')) {
43
            $metadata = $this->objectManager->getClassMetadata($this->class);
44
            $this->class = $metadata->getName();
45
        }
46
        return $this->class;
47
    }
48
49
    /**
50
     * @return CallbackInterface
51
     */
52
    public function createCallback() : CallbackInterface
53
    {
54
        $class = $this->getClass();
55
        $obj = new $class();
56
        return $obj;
57
    }
58
59
    /**
60
     * @param Request $request
61
     * @return CallbackInterface
62
     */
63
    public function createCallbackFromRequest(Request $request) : CallbackInterface
64
    {
65
        $fields = [
66
            'shop_orderid' => 'orderId',
67
            'amount' => 'amount',
68
            'currency' => 'currency',
69
            'language' => 'language',
70
            'transaction_info' => 'transactionInfo',
71
            'status' => 'status',
72
            'error_message' => 'errorMessage',
73
            'merchant_error_message' => 'merchantErrorMessage',
74
            'cardholder_message_must_be_shown' => 'cardholderMessageMustBeShown',
75
            'transaction_id' => 'transactionId',
76
            'type' => 'type',
77
            'payment_status' => 'paymentStatus',
78
            'masked_credit_card' => 'maskedCreditCard',
79
            'blacklist_token' => 'blacklistToken',
80
            'credit_card_token' => 'creditCardToken',
81
            'nature' => 'nature',
82
            'require_capture' => 'requireCapture',
83
            'xml' => 'xml',
84
            'checksum' => 'checksum',
85
            'fraud_risk_score' => 'fraudRiskScore',
86
            'fraud_explanation' => 'fraudExplanation',
87
            'fraud_recommendation' => 'fraudRecommendation',
88
            'avs_code' => 'avsCode',
89
            'avs_text' => 'avsText',
90
        ];
91
        $obj = $this->createCallback();
92
        $obj->setRequest((string)$request);
93
94
        foreach ($fields as $key => $field) {
95
            $val = $request->get($key);
96
            if($val) {
97
                $setter = 'set'.ucfirst($field);
98
                $obj->{$setter}($val);
99
            }
100
        }
101
102
        return $obj;
103
    }
104
105
    /**
106
     * @param CallbackInterface $callback
107
     */
108
    public function deleteCallback(CallbackInterface $callback)
109
    {
110
        $this->objectManager->remove($callback);
111
        $this->objectManager->flush();
112
    }
113
114
    /**
115
     * @param CallbackInterface $callback
116
     * @param bool $flush
117
     */
118
    public function updateCallback(CallbackInterface $callback, bool $flush = true)
119
    {
120
        $this->objectManager->persist($callback);
121
122
        if($flush) {
123
            $this->objectManager->flush();
124
        }
125
    }
126
}