Completed
Push — master ( 4da3a0...4ef8bc )
by Matt
09:23
created

ExifHelper::getCreationDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 13
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 = []; // Match the default in our Image entity
42
        $gps_as_string = $this->exif->getGPS();
43
        if (is_string($gps_as_string)) {
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 []; // Match the default in our Image entity
60
    }
61
    public function getCreationDate():?\DateTime
62
    {
63
        $creationDate = $this->exif->getCreationDate();
64
        if ($creationDate instanceof \DateTime) {
65
            // PHPExif assumes that the camera time string is UTC. It's not for many
66
            // people's cameras, including mine. Re-interpret it as local time in
67
            // Bristol. Not much of a bodge given that this project is specifically
68
            // limited to a small geographical region!
69
            $converted = new \DateTime($creationDate->format("Y-m-d\TH:i:s"), new \DateTimeZone("Europe/London"));
70
            $converted->setTimezone(new \DateTimeZone("UTC"));
71
            return $converted;
72
        }
73
        return null;
74
    }
75
    public function getRating():?int
76
    {
77
        $raw = $this->exif->getRawData();
78
        if (array_key_exists('XMP-xmp:Rating', $raw)) {
79
            $rating = $raw['XMP-xmp:Rating'];
80
            if (is_int($rating)) {
81
                return $rating;
82
            }
83
        }
84
        return null;
85
    }
86
    public function getLocation(): ?string
87
    {
88
        $raw = $this->exif->getRawData();
89
        if (array_key_exists('IPTC:Sub-location', $raw)) {
90
            $location = $raw['IPTC:Sub-location'];
91
            if (is_string($location)) {
92
                return $location;
93
            }
94
        }
95
        return null;
96
    }
97
}