createPersistentDataHandler()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 6
nop 1
dl 0
loc 20
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Maztech\PersistentData;
4
5
use InvalidArgumentException;
6
7
class PersistentDataFactory
8
{
9
    private function __construct()
10
    {
11
        // a factory constructor should never be invoked
12
    }
13
14
    /**
15
     * PersistentData generation.
16
     *
17
     * @param PersistentDataInterface|string|null $handler
18
     *
19
     * @throws InvalidArgumentException If the persistent data handler isn't "session", "memory", or an instance of Instagram\PersistentData\PersistentDataInterface.
20
     *
21
     * @return PersistentDataInterface
22
     */
23
    public static function createPersistentDataHandler($handler)
24
    {
25
        if (!$handler) {
26
            return session_status() === PHP_SESSION_ACTIVE
27
                ? new InstagramSessionPersistentDataHandler()
28
                : new InstagramMemoryPersistentDataHandler();
29
        }
30
31
        if ($handler instanceof PersistentDataInterface) {
32
            return $handler;
33
        }
34
35
        if ('session' === $handler) {
36
            return new InstagramSessionPersistentDataHandler();
37
        }
38
        if ('memory' === $handler) {
39
            return new InstagramMemoryPersistentDataHandler();
40
        }
41
42
        throw new InvalidArgumentException('The persistent data handler must be set to "session", "memory", or be an instance of Instagram\PersistentData\PersistentDataInterface');
43
    }
44
}
45