Passed
Push — master ( e09aba...f9ce3a )
by Matt
04:00
created

ExifHelper::getRating()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace App\Utils;
4
5
use PHPExif\Exif;
6
7
/**
8
 * Wrapper for PHPExif's Exif class, which is a little annoying and
9
 * also has incorrect PHPDoc in a couple of places. Basically translates
10
 * all calls we use to return exactly the type we need, or null, to
11
 * make it nice and compatible with our existing code.
12
 */
13
class ExifHelper implements ExifHelperInterface
14
{
15
    /** @var Exif */
16
    private $exif;
17
18
    public function __construct(Exif $exif)
19
    {
20
        $this->exif = $exif;
21
    }
22
23
    public function getTitle():?string
24
    {
25
        $title = $this->exif->getTitle();
26
        if (is_string($title)) {
27
            return $title;
28
        }
29
        return null;
30
    }
31
    public function getDescription():?string
32
    {
33
        $description = $this->exif->getCaption();
34
        if (is_string($description)) {
35
            return $description;
36
        }
37
        return null;
38
    }
39
    public function getGPS():?array
40
    {
41
        $gps = null;
42
        $gps_as_string = $this->exif->getGPS();
43
        if (is_string($gps_as_string)) {
1 ignored issue
show
introduced by
The condition is_string($gps_as_string) is always false.
Loading history...
44
            $gps = array_map('doubleval', explode(',', $gps_as_string));
45
        }
46
        return $gps;
47
    }
48
    public function getKeywords():?array
49
    {
50
        $keywords = $this->exif->getKeywords();
51
        if (is_string($keywords)) {
0 ignored issues
show
introduced by
The condition is_string($keywords) is always false.
Loading history...
52
            // A single keyword comes back as a simple string, not an array. We
53
            // always return an array.
54
            $keywords = [ $keywords ];
55
        }
56
        if (is_array($keywords)) {
57
            return $keywords;
58
        }
59
        return null;
60
    }
61
    public function getCreationDate():?\DateTime
62
    {
63
        $creationDate = $this->exif->getCreationDate();
64
        if ($creationDate instanceof \DateTime) {
65
            return $creationDate;
66
        }
67
        return null;
68
    }
69
    public function getRating():?int
70
    {
71
        $raw = $this->exif->getRawData();
72
        if (array_key_exists('XMP-xmp:Rating', $raw)) {
73
            $rating = $raw['XMP-xmp:Rating'];
74
            if (is_int($rating)) {
75
                return $rating;
76
            }
77
        }
78
        return null;
79
    }
80
81
}