Payone_Api_Request_Abstract::getMid()   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 0
1
<?php
2
/**
3
 *
4
 * NOTICE OF LICENSE
5
 *
6
 * This source file is subject to the GNU General Public License (GPL 3)
7
 * that is bundled with this package in the file LICENSE.txt
8
 *
9
 * DISCLAIMER
10
 *
11
 * Do not edit or add to this file if you wish to upgrade Payone to newer
12
 * versions in the future. If you wish to customize Payone for your
13
 * needs please refer to http://www.payone.de for more information.
14
 *
15
 * @category        Payone
16
 * @package         Payone_Api
17
 * @subpackage      Request
18
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
19
 * @author          Matthias Walter <[email protected]>
20
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
21
 * @link            http://www.noovias.com
22
 */
23
24
/**
25
 *
26
 * @category        Payone
27
 * @package         Payone_Api
28
 * @subpackage      Request
29
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
30
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
31
 * @link            http://www.noovias.com
32
 */
33
34
35
abstract class Payone_Api_Request_Abstract
36
    implements Payone_Api_Request_Interface
37
{
38
    /**
39
     * @var int
40
     */
41
    protected $mid = NULL;
42
43
    /**
44
     * @var int
45
     */
46
    protected $portalid = NULL;
47
48
    /**
49
     * @var string
50
     */
51
    protected $key = NULL;
52
53
    /**
54
     * @var string
55
     */
56
    protected $mode = NULL;
57
58
    /**
59
     * @var string
60
     */
61
    protected $request = NULL;
62
63
    /**
64
     * @var string
65
     */
66
    protected $encoding = NULL;
67
68
    /**
69
     * name of the solution-partner (company)
70
     *
71
     * @var string
72
     */
73
    protected $solution_name = NULL;
74
75
    /**
76
     * version of the solution-partner's app / extension / plugin / etc..
77
     *
78
     * @var string
79
     */
80
    protected $solution_version = NULL;
81
82
    /**
83
     * system-name
84
     *
85
     * @var string
86
     */
87
    protected $integrator_name = NULL;
88
89
    /**
90
     * system-version
91
     *
92
     * @var string
93
     */
94
    protected $integrator_version = NULL;
95
96
    /**
97
     * @var Payone_Protocol_Service_ApplyFilters
98
     */
99
    private $applyFilters = NULL;
100
    
101
    /**
102
     * @param array $data
103
     */
104
    public function __construct(array $data = array())
105
    {
106
        if (count($data) > 0) {
107
            $this->init($data);
108
        }
109
    }
110
111
    /**
112
     * @param array $data
113
     */
114 View Code Duplication
    public function init(array $data = array())
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...
115
    {
116
        foreach ($data as $key => $value)
117
        {
118
            $key = ucwords(str_replace('_', ' ', $key));
119
            $method = 'set' . str_replace(' ', '', $key);
120
121
            if (method_exists($this, $method)) {
122
                $this->{$method}($value);
123
            }
124
        }
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    public function toArray()
131
    {
132
        $result = array();
133
        foreach ($this as $key => $data)
0 ignored issues
show
Bug introduced by
The expression $this of type this<Payone_Api_Request_Abstract> is not traversable.
Loading history...
134
        {
135
            if ($data === null) {
136
                continue;
137
            }
138
139
            if ($data instanceof Payone_Api_Request_Parameter_Interface) {
140
                /**
141
                 * @var Payone_Api_Request_Parameter_Interface $data
142
                 */
143
                $result = array_merge($result, $data->toArray());
144
            }
145
            elseif ($data instanceof Payone_Protocol_Service_ApplyFilters == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
146
                $result[$key] = $data;
147
            }
148
        }
149
150
        ksort($result);
151
152
        return $result;
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function __toString()
159
    {
160
        if($this->applyFilters) {
161
            $result = $this->applyFilters->apply($this->toArray());
162
        } else {
163
            $protocolFactory     = new Payone_Protocol_Factory();
164
            $defaultApplyFilters = $protocolFactory->buildServiceApplyFilters();
165
            $result = $defaultApplyFilters->apply($this->toArray());
166
        }
167
168
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (string) is incompatible with the return type declared by the interface Payone_Api_Request_Interface::__toString of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
169
    }
170
171
172
    /**
173
     * @param $name
174
     * @return null|mixed
175
     */
176 View Code Duplication
    public function get($name)
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...
177
    {
178
        if (strpos($name, '/') !== false) {
179
            $explodedName = explode('/', $name);
180
            if (count($explodedName) != 2) {
181
                return null;
182
            }
183
184
            $property = $explodedName[0];
185
            $propertyName = $explodedName[1];
186
            if (property_exists($this, $property)) {
187
                $object = $this->$property;
188
                /**
189
                 * @var $object Payone_Api_Request_Parameter_Interface
190
                 */
191
                if (!($object instanceof Payone_Api_Request_Parameter_Interface)) {
192
                    return null;
193
                }
194
195
                return $object->get($propertyName);
196
            }
197
        }
198
        elseif (property_exists($this, $name)) {
199
            return $this->$name;
200
        }
201
202
        return null;
203
    }
204
205
    /**
206
     * @param string $name
207
     * @param mixed $value
208
     * @return boolean|null
209
     */
210 View Code Duplication
    public function set($name, $value)
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...
211
    {
212
        if (strpos($name, '/') !== false) {
213
            $explodedName = explode('/', $name);
214
            if (count($explodedName) != 2) {
215
                return null;
216
            }
217
218
            $property = $explodedName[0];
219
            $propertyName = $explodedName[1];
220
            if (property_exists($this, $property)) {
221
                $object = $this->$property;
222
                /**
223
                 * @var $object Payone_Api_Request_Parameter_Interface
224
                 */
225
                if (!($object instanceof Payone_Api_Request_Parameter_Interface)) {
226
                    return null;
227
                }
228
229
                return $object->set($propertyName, $value);
230
            }
231
        }
232
        elseif (property_exists($this, $name)) {
233
            $this->$name = $value;
234
            return true;
235
        }
236
237
        return null;
238
    }
239
240
    /**
241
     * @param string $encoding
242
     */
243
    public function setEncoding($encoding)
244
    {
245
        $this->encoding = $encoding;
246
    }
247
248
    /**
249
     * @return string
250
     */
251
    public function getEncoding()
252
    {
253
        return $this->encoding;
254
    }
255
256
    /**
257
     * @param string $key
258
     */
259
    public function setKey($key)
260
    {
261
        $this->key = md5($key);
262
    }
263
264
    /**
265
     * @return string
266
     */
267
    public function getKey()
268
    {
269
        return $this->key;
270
    }
271
272
    /**
273
     * @param int $mid
274
     */
275
    public function setMid($mid)
276
    {
277
        $this->mid = $mid;
278
    }
279
280
    /**
281
     * @return int
282
     */
283
    public function getMid()
284
    {
285
        return $this->mid;
286
    }
287
288
    /**
289
     * @param string $mode
290
     */
291
    public function setMode($mode)
292
    {
293
        $this->mode = $mode;
294
    }
295
296
    /**
297
     * @return string
298
     */
299
    public function getMode()
300
    {
301
        return $this->mode;
302
    }
303
304
    /**
305
     * @param int $portalid
306
     */
307
    public function setPortalid($portalid)
308
    {
309
        $this->portalid = $portalid;
310
    }
311
312
    /**
313
     * @return int
314
     */
315
    public function getPortalid()
316
    {
317
        return $this->portalid;
318
    }
319
320
    /**
321
     * @param string $request
322
     */
323
    public function setRequest($request)
324
    {
325
        $this->request = $request;
326
    }
327
328
    /**
329
     * @return string
330
     */
331
    public function getRequest()
332
    {
333
        return $this->request;
334
    }
335
336
    /**
337
     * set the system-Name
338
     *
339
     * @param string $integrator_name
340
     */
341
    public function setIntegratorName($integrator_name)
342
    {
343
        $this->integrator_name = $integrator_name;
344
    }
345
346
    /**
347
     * @return string
348
     */
349
    public function getIntegratorName()
350
    {
351
        return $this->integrator_name;
352
    }
353
354
    /**
355
     * set the system-version
356
     *
357
     * @param string $integrator_version
358
     */
359
    public function setIntegratorVersion($integrator_version)
360
    {
361
        $this->integrator_version = $integrator_version;
362
    }
363
364
    /**
365
     * @return string
366
     */
367
    public function getIntegratorVersion()
368
    {
369
        return $this->integrator_version;
370
    }
371
372
    /**
373
     * set the name of the solution-partner (company)
374
     *
375
     * @param string $solution_name
376
     */
377
    public function setSolutionName($solution_name)
378
    {
379
        $this->solution_name = $solution_name;
380
    }
381
382
    /**
383
     * @return string
384
     */
385
    public function getSolutionName()
386
    {
387
        return $this->solution_name;
388
    }
389
390
    /**
391
     * set the version of the solution-partner's app / extension / plugin / etc..
392
     *
393
     * @param string $solution_version
394
     */
395
    public function setSolutionVersion($solution_version)
396
    {
397
        $this->solution_version = $solution_version;
398
    }
399
400
    /**
401
     * @return string
402
     */
403
    public function getSolutionVersion()
404
    {
405
        return $this->solution_version;
406
    }
407
408
    /**
409
     * @param Payone_Protocol_Service_ApplyFilters $applyFilters
410
     */
411
    public function setApplyFilters(Payone_Protocol_Service_ApplyFilters $applyFilters)
412
    {
413
        $this->applyFilters = $applyFilters;
414
    }
415
    
416
    public function isFrontendApiCall() 
417
    {
418
        if($this instanceof Payone_Api_Request_Authorization_Abstract) {
419
            $oOrder = Mage::getSingleton('checkout/session')->getQuote();
420
            $oPayment = $oOrder->getPayment();
421
            if($oPayment->getMethod() == 'payone_creditcard_iframe') {
422
                return true;
423
            }
424
        }
425
426
        return false;
427
    }
428
    
429
}
430