GatewaysOptionsHandler::getGatewayOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace XoopsModules\Oledrion;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * oledrion
17
 *
18
 * @copyright   {@link https://xoops.org/ XOOPS Project}
19
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
21
 */
22
23
use XoopsModules\Oledrion;
24
25
/**
26
 * Managing payment gateway options
27
 */
28
29
/**
30
 * Class GatewaysOptionsHandler
31
 */
32
class GatewaysOptionsHandler extends OledrionPersistableObjectHandler
33
{
34
    /**
35
     * GatewaysOptionsHandler constructor.
36
     * @param \XoopsDatabase|null $db
37
     */
38
    public function __construct(\XoopsDatabase $db = null)
39
    {
40
        //                                Table                       Classe                      Id
41
        parent::__construct($db, 'oledrion_gateways_options', GatewaysOptions::class, 'option_id');
0 ignored issues
show
Bug introduced by
It seems like $db can also be of type null; however, parameter $db of XoopsModules\Oledrion\Ol...tHandler::__construct() does only seem to accept XoopsDatabase, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        parent::__construct(/** @scrutinizer ignore-type */ $db, 'oledrion_gateways_options', GatewaysOptions::class, 'option_id');
Loading history...
42
    }
43
44
    /**
45
     * Returns all the options of a payment gateway
46
     *
47
     * @param  string $option_gateway The name of the payment gateway
48
     * @return array  Array of GatewaysOptions objects
49
     */
50
    public function getGatewayOptions($option_gateway)
51
    {
52
        $criteria = new \Criteria('option_gateway', $option_gateway, '=');
53
54
        return $this->getObjects($criteria);
55
    }
56
57
    /**
58
     * Removes all options from a payment gateway
59
     *
60
     * @param  string $option_gateway
61
     * @return bool The result of removing options
62
     */
63
    public function deleteGatewayOptions($option_gateway)
64
    {
65
        $criteria = new \Criteria('option_gateway', $option_gateway, '=');
66
67
        return $this->deleteAll($criteria);
68
    }
69
70
    /**
71
     * Returns an option of a payment gateway
72
     *
73
     * @param  string $option_gateway The name of the payment gateway
74
     * @param  string $option_name    The option you want to recover
75
     * @return array Objects of type GatewaysOptions
76
     */
77
    public function getGatewayOption($option_gateway, $option_name)
78
    {
79
        $criteria = new \CriteriaCompo();
80
        $criteria->add(new \Criteria('option_gateway', $option_gateway, '='));
81
        $criteria->add(new \Criteria('option_name', $option_name, '='));
82
83
        return $this->getObjects($criteria);
84
    }
85
86
    /**
87
     * Returns the VALUE of an option of a payment gateway
88
     *
89
     * @param  string $option_gateway he name of a payment gateway
90
     * @param  string $option_name    The option you want to recover
91
     * @param  string $format         The format in which we want to recover the value (in relation to getVar())
92
     * @param  bool   $unserialize    Whether to deserialize the return value
93
     * @return mixed   The value of the option or null if the option can not be found
94
     */
95
    public function getGatewayOptionValue($option_gateway, $option_name, $format = 'N', $unserialize = false)
96
    {
97
        $ret      = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
98
        $criteria = new \CriteriaCompo();
99
        $criteria->add(new \Criteria('option_gateway', $option_gateway, '='));
100
        $criteria->add(new \Criteria('option_name', $option_name, '='));
101
        $ret = $this->getObjects($criteria);
102
        if (count($ret) > 0) {
103
            if ($unserialize) {
104
                return unserialize($ret[0]->getVar('option_value', $format));
105
            }
106
107
            return $ret[0]->getVar('option_value', $format);
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * Positions the value of an option of a payment gateway and saves it
115
     *
116
     * @param  string $option_gateway The name of the payment gateway
117
     * @param  string $option_name    The name of the option
118
     * @param  mixed  $option_value   The value of the option
119
     * @param  bool   $serialize      Whether to serialize the value before saving
120
     * @return bool The result of the update (true if the update was made otherwise false)
121
     */
122
    public function setGatewayOptionValue($option_gateway, $option_name, $option_value, $serialize = false)
123
    {
124
        $ret      = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $ret is dead and can be removed.
Loading history...
125
        $criteria = new \CriteriaCompo();
126
        $criteria->add(new \Criteria('option_gateway', $option_gateway, '='));
127
        $criteria->add(new \Criteria('option_name', $option_name, '='));
128
        $ret = $this->getObjects($criteria);
129
        if (count($ret) > 0) {
130
            $option = $ret[0];
131
            if ($serialize) {
132
                $option->setVar('option_value', serialize($option_value));
133
            } else {
134
                $option->setVar('option_value', $option_value);
135
            }
136
137
            return $this->insert($option, true);
138
        }
139
140
        // Option not found, we will create it
141
        $option = $this->create(true);
142
        $option->setVar('option_gateway', $option_gateway);
143
        $option->setVar('option_name', $option_name);
144
        $option->setVar('option_value', $option_value);
145
146
        return $this->insert($option, true);
147
    }
148
}
149