MongoDbUtil::ensureMongoDate()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 8
cp 0
rs 9.4222
c 0
b 0
f 0
cc 5
nc 6
nop 1
crap 30
1
<?php
2
/**
3
 * File was created 11.04.2016 08:54
4
 */
5
6
namespace PeekAndPoke\Component\Slumber\Data\MongoDb;
7
8
use MongoDB\BSON\ObjectID;
9
use MongoDB\BSON\UTCDateTime;
10
use PeekAndPoke\Component\Psi\Psi\IsDateString;
11
use PeekAndPoke\Component\Toolbox\ArrayUtil;
12
use PeekAndPoke\Types\LocalDate;
13
14
/**
15
 * @author Karsten J. Gerber <[email protected]>
16
 */
17
class MongoDbUtil
18
{
19
    /**
20
     * Ensures that the input will be transformed in to a keyless array.
21
     *
22
     * Why? Well when you have an array like
23
     *
24
     * [0 => 1, 2 => 2]
25
     *
26
     * it will converted to a json object in a query:
27
     *
28
     * {"0" : 1, "2" : 2}
29
     *
30
     * while you probably wanted to query with
31
     *
32
     * [1, 2]
33
     *
34
     * @param $input
35
     *
36
     * @return array
37
     */
38
    public static function ensureList($input)
39
    {
40
        return array_values(
41
            ArrayUtil::ensureArray($input)
42
        );
43
    }
44
45
    /**
46
     * @param mixed $id
47
     *
48
     * @return bool
49
     */
50 32
    public static function isValidMongoIdString($id)
51
    {
52 32
        return $id !== null && \strlen($id) === 24 && preg_match('/[a-fA-F0-9]{24}/', $id);
53
    }
54
55
    /**
56
     * @param mixed $subject
57
     *
58
     * @return ObjectID|mixed
59
     */
60 33
    public static function ensureMongoId($subject)
61
    {
62 33
        if ($subject instanceof ObjectID) {
0 ignored issues
show
Bug introduced by
The class MongoDB\BSON\ObjectID does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
63 6
            return $subject;
64
        }
65
66
        /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
67 32
        return self::isValidMongoIdString($subject) ? new ObjectID($subject) : $subject;
68
    }
69
70
    /**
71
     * @param LocalDate|\DateTime|string $subject
72
     *
73
     * @return UTCDateTime|null
74
     */
75
    public static function ensureMongoDate($subject)
76
    {
77
        if (is_scalar($subject) && IsDateString::isValidDateString($subject)) {
78
            $subject = new \DateTime($subject);
79
        }
80
81
        if ($subject instanceof \DateTime) {
82
            return new UTCDateTime($subject->getTimestamp() * 1000);
83
        }
84
85
        if ($subject instanceof LocalDate) {
86
            return new UTCDateTime($subject->getTimestamp() * 1000);
87
        }
88
89
        return null;
90
    }
91
}
92