Passed
Push — master ( e5f64f...dec0ac )
by Jason
01:47
created

Transaction::getDecryptedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dynamic\Foxy\Foxy;
4
5
use Dynamic\Foxy\Model\FoxyHelper;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\ORM\ValidationException;
8
9
/**
10
 * Class Transaction
11
 * @package Dynamic\Foxy\Foxy
12
 */
13
class Transaction
14
{
15
    use Injectable;
16
17
    /**
18
     * @var
19
     */
20
    private $transaction;
21
22
    /**
23
     * Transaction constructor.
24
     * @param $transactionID
25
     * @param $data
26
     * @throws ValidationException
27
     */
28
    public function __construct($transactionID, $data)
29
    {
30
        $this->setTransaction($transactionID, $data);
31
    }
32
33
    /**
34
     * @param $transactionID
35
     * @param $data
36
     * @return $this
37
     * @throws ValidationException
38
     */
39
    public function setTransaction($transactionID, $data)
40
    {
41
        $decryptedData = $this->getDecryptedData($data);
42
43
        foreach ($decryptedData->transactions->transaction as $transaction) {
44
            if ($transactionID == (int)$transaction->id) {
45
                $this->transaction = $transaction;
46
                break;
47
            }
48
        }
49
50
        if (!$this->transaction) {
51
            $this->transaction = false;
52
        }
53
54
        return $this;
55
    }
56
57
    /**
58
     * @return mixed
59
     */
60
    public function getTransaction()
61
    {
62
        return $this->transaction;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function exists()
69
    {
70
        return $this->getTransaction() != false && !empty($this->getTransaction());
71
    }
72
73
    /**
74
     * @param $data
75
     * @return \SimpleXMLElement
76
     * @throws \SilverStripe\ORM\ValidationException
77
     */
78
    private function getDecryptedData($data)
79
    {
80
        $helper = new FoxyHelper();
81
        return new \SimpleXMLElement(\rc4crypt::decrypt($helper->config()->get('secret'), $data));
82
    }
83
}
84