Completed
Push — develop ( 19c595...0fe9d6 )
by Seth
06:00
created

Syncable::getSyncTimestamp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 10
nc 2
nop 0
1
<?php
2
3
namespace smtech\CanvasICSSync\SyncIntoCanvas;
4
5
use PDO;
6
use smtech\CanvasPest\CanvasPest;
7
8
/**
9
 * A syncable object
10
 *
11
 * @author Seth Battis <[email protected]>
12
 */
13
abstract class Syncable
14
{
15
    /**
16
     * Database connection
17
     *
18
     * @var PDO|null
19
     */
20
    protected static $database;
21
22
    /**
23
     * Canvas API connection
24
     *
25
     * @var CanvasPest|null
26
     */
27
    protected static $api;
28
29
    /**
30
     * Unique timestamp for the current sync
31
     * @var string
32
     */
33
    protected static $syncTimestamp;
34
35
    /**
36
     * Generate a unique identifier for this synchronization pass
37
     **/
38
    public static function getSyncTimestamp()
0 ignored issues
show
Coding Style introduced by
getSyncTimestamp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
39
    {
40
        if (empty(static::$syncTimestamp)) {
41
            $timestamp = new DateTime();
42
            static::$syncTimestamp =
43
                $timestamp->format(Constants::SYNC_TIMESTAMP_FORMAT) .
44
                Constants::SEPARATOR . md5(
45
                    (php_sapi_name() == 'cli' ?
46
                        'cli' : $_SERVER['REMOTE_ADDR']
47
                    ) . time()
48
                );
49
        }
50
        return static::$syncTimestamp;
51
    }
52
53
    /**
54
     * Update the MySQL connection
55
     *
56
     * @param PDO $db
0 ignored issues
show
Bug introduced by
There is no parameter named $db. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
57
     * @throws Exception If `$db` is null
58
     */
59
    public static function setDatabase(PDO $database)
60
    {
61
        if (empty($database)) {
62
            throw new Exception(
63
                'A non-null database connection is required'
64
            );
65
        } else {
66
            static::$database = $database;
67
        }
68
    }
69
70
    /**
71
     * Get the MySQL connection
72
     *
73
     * @return mysqli
74
     */
75
    public static function getDatabase()
76
    {
77
        return static::$db;
78
    }
79
80
    /**
81
     * Update the API connection
82
     *
83
     * @param CanvasPest $api
84
     * @throws Exception if `$api` is null
85
     */
86
    public static function setApi(CanvasPest $api)
87
    {
88
        if (empty($api)) {
89
            throw new Exception(
90
                'A non-null API connection is required'
91
            );
92
        } else {
93
            static::$api = $api;
94
        }
95
    }
96
97
    /**
98
     * Get the API connection
99
     *
100
     * @return CanvasPest|null
101
     */
102
    public static function getApi()
103
    {
104
        return static::$api;
105
    }
106
107
    /**
108
     * Save the syncable object to the MySQL database
109
     *
110
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
111
     */
112
    abstract public function save();
113
114
    /**
115
     * Load a syncable object from the MySQL database
116
     *
117
     * @param int $id
118
     * @return Syncable|null
119
     */
120
    abstract public static function load($id);
121
}
122