GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — develop (#27)
by Tom Van
04:06
created

Reader::getMapper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Reader which uses native PHP functionality to read EXIF data
4
 *
5
 * @category    PHPExif
6
 * @copyright   Copyright (c) 2016 Tom Van Herreweghe <[email protected]>
7
 * @license     http://github.com/PHPExif/php-exif-native/blob/master/LICENSE MIT License
8
 * @link        http://github.com/PHPExif/php-exif-native for the canonical source repository
9
 * @package     Native
10
 */
11
12
namespace PHPExif\Adapter\Native;
13
14
use PHPExif\Common\Adapter\MapperInterface;
15
use PHPExif\Common\Adapter\ReaderInterface;
16
use PHPExif\Common\Data\Exif;
17
use PHPExif\Common\Data\Iptc;
18
use PHPExif\Common\Data\Metadata;
19
use PHPExif\Common\Exception\Reader\NoExifDataException;
20
21
/**
22
 * Reader
23
 *
24
 * Reads EXIF data
25
 *
26
 * @category    PHPExif
27
 * @package     Native
28
 */
29
final class Reader implements ReaderInterface
30
{
31
    const SECTIONS = 'sections';
32
    const ARRAYS = 'asArrays';
33
    const THUMBNAILINFO = 'thumbnailInfo';
34
    const WITHIPTC = 'withIptc';
35
36
    /**
37
     * @var MapperInterface
38
     */
39
    private $mapper;
40
41
    /**
42
     * @var string
43
     */
44
    private $sections;
45
46
    /**
47
     * @var boolean
48
     */
49
    private $asArrays;
50
51
    /**
52
     * @var boolean
53
     */
54
    private $thumbnailInfo;
55
56
    /**
57
     * @var boolean
58
     */
59
    private $withIptc;
60
61
    /**
62
     * @param MapperInterface $mapper
63
     * @param array $config
64
     */
65
    public function __construct(
66
        MapperInterface $mapper,
67
        array $config = []
68
    ) {
69
        $defaults = [
70
            self::SECTIONS => null,
71
            self::ARRAYS => false,
72
            self::THUMBNAILINFO => false,
73
            self::WITHIPTC => true,
74
        ];
75
76
        $config = array_replace($defaults, $config);
77
78
        $this->sections = $config[self::SECTIONS];
79
        $this->asArrays = (bool) $config[self::ARRAYS];
80
        $this->thumbnailInfo = (bool) $config[self::THUMBNAILINFO];
81
        $this->withIptc = (bool) $config[self::WITHIPTC];
82
83
        $this->mapper = $mapper;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function getMapper()
90
    {
91
        return $this->mapper;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function getMetadataFromFile($filePath)
98
    {
99
        $data = @exif_read_data(
100
            $filePath,
101
            $this->sections,
102
            $this->asArrays, // flat array
103
            $this->thumbnailInfo // no thumbnail
104
        );
105
106
        if (false === $data) {
107
            throw NoExifDataException::fromFile($filePath);
108
        }
109
110
        $this->augmentDataWithIptcRawData($filePath, $data);
111
112
        $data = $this->normalizeArrayKeys($data);
113
114
        // map the data:
115
        $mapper = $this->getMapper();
116
        $metadata = new Metadata(
117
            new Exif,
118
            new Iptc
119
        );
120
        $mapper->map($data, $metadata);
121
122
        return $metadata;
123
    }
124
125
    /**
126
     * Lowercases the keys for given array
127
     *
128
     * @param array $data
129
     *
130
     * @return array
131
     */
132
    private function normalizeArrayKeys(array $data)
133
    {
134
        $keys = array_keys($data);
135
        $keys = array_map('strtolower', $keys);
136
        $values = array_values($data);
137
        $values = array_map(function ($value) {
138
            if (!is_array($value)) {
139
                return $value;
140
            }
141
142
            return $this->normalizeArrayKeys($value);
143
        }, $values);
144
145
        return array_combine(
146
            $keys,
147
            $values
148
        );
149
    }
150
151
    /**
152
     * Adds data from iptcparse to the original raw EXIF data
153
     *
154
     * @param string $filePath
155
     * @param array $data
156
     *
157
     * @return void
158
     */
159
    private function augmentDataWithIptcRawData($filePath, array &$data)
160
    {
161
        if (!$this->withIptc) {
162
            return;
163
        }
164
165
        getimagesize($filePath, $info);
166
167
        if (!array_key_exists('APP13', $info)) {
168
            return;
169
        }
170
171
        $iptcRawData = iptcparse($info['APP13']);
172
173
        // UTF8
174
        if (isset($iptcRawData["1#090"]) && $iptcRawData["1#090"][0] == "\x1B%G") {
175
            $iptcRawData = array_map('utf8_encode', $iptcRawData);
176
        }
177
178
        // Merge with original raw Exif data
179
        $data = array_merge(
180
            $data,
181
            $iptcRawData
182
        );
183
    }
184
}
185