Completed
Push — master ( 11a657...cb88da )
by Gordon
10:22
created

ImageMetaDataExtension   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.94%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 15
c 1
b 1
f 0
lcom 1
cbo 4
dl 0
loc 116
ccs 75
cts 79
cp 0.9494
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
F processExifData() 0 80 11
A onAfterWrite() 0 8 2
A requireDefaultRecords() 0 10 2
1
<?php
2
3
class ImageMetaDataExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
6
        'ExifRead' => 'Boolean',
7
        'Aperture' => 'Varchar',
8
        'ShutterSpeed' => 'Varchar',
9
        'TakenAt' => 'Datetime',
10
        'ISO' => 'Int',
11
        'Orientation' => 'Int'
12
    );
13
14
15
    public static $defaults = array('ExifRead' => false);
16
17 15
    public function processExifData()
18
    {
19 15
        $image    = $this->owner->Image();
20 15
        $filename = BASE_PATH . '/' . $image->Filename;
21
22
        // when the image is first saved, the file will still be a temp file
23 15
        if ($image->exists()) {
24
            try {
25 2
                $exif = exif_read_data($filename, null, true);
26 2
                error_log(print_r($exif, 1));
27 2
                if (isset($exif['COMPUTED']['ApertureFNumber'])) {
28 2
                    $aperture = $exif['COMPUTED']['ApertureFNumber'];
29 2
                    $aperture = str_replace('f/', '', $aperture);
30 2
                    $this->owner->Aperture = $aperture;
31 2
                }
32
33
34 2
                $shutterspeed = '';
35 2
                if (isset($exif['ExposureTime'])) {
36
                    $shutterspeed = $exif['ExposureTime'];
37 2
                } elseif (isset($exif['EXIF']['ExposureTime'])) {
38 2
                    $shutterspeed = $exif['EXIF']['ExposureTime'];
39 2
                }
40
41 2
                $this->owner->ShutterSpeed = $shutterspeed;
42 2
                if (isset($exif['DateTimeOriginal'])) {
43
                    $this->owner->TakenAt = $exif['DateTimeOriginal'];
44 2
                } elseif (isset($exif['EXIF']['DateTimeOriginal'])) {
45 2
                    $this->owner->TakenAt = $exif['EXIF']['DateTimeOriginal'];
46 2
                }
47
48 2
                $iso = '';
49 2
                if (isset($exif['ISOSpeedRatings'])) {
50
                    $iso = $exif['ISOSpeedRatings'];
51 2
                } elseif (isset($exif['EXIF']['ISOSpeedRatings'])) {
52 2
                    $iso = $exif['EXIF']['ISOSpeedRatings'];
53 2
                }
54
55 2
                $this->owner->ISO = $iso;
56
57 2
                if (isset($exif['GPS'])) {
58 2
                    $gps      = $exif['GPS'];
59 2
                    $latarray = $gps['GPSLatitude'];
60 2
                    $degrees  = $latarray[0];
61 2
                    $parts    = explode('/', $degrees);
62 2
                    $degrees  = $parts[0] / $parts[1];
63 2
                    $minutes  = $latarray[1];
64 2
                    $parts    = explode('/', $minutes);
65 2
                    $minutes  = $parts[0] / $parts[1];
66 2
                    $seconds  = $latarray[2];
67 2
                    $parts    = explode('/', $seconds);
68 2
                    $seconds  = $parts[0] / $parts[1];
69 2
                    $latitude = $degrees + $minutes / 60 + $seconds / 3600;
70 2
                    $lonarray = $gps['GPSLongitude'];
71 2
                    $degrees  = $lonarray[0];
72 2
                    $parts    = explode('/', $degrees);
73 2
                    $degrees  = $parts[0] / $parts[1];
74 2
                    $minutes  = $lonarray[1];
75 2
                    $parts    = explode('/', $minutes);
76 2
                    $minutes  = $parts[0] / $parts[1];
77 2
                    $seconds  = $lonarray[2];
78 2
                    $parts    = explode('/', $seconds);
79 2
                    $seconds  = $parts[0] / $parts[1];
80
81 2
                    $longitude        = $degrees + $minutes / 60 + $seconds / 3600;
82 2
                    $this->owner->Lat = $latitude;
83 2
                    $this->owner->Lon = $longitude;
84 2
                }
85
86 2
                $image = $this->owner->Image();
0 ignored issues
show
Unused Code introduced by
$image is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
88 2
                $this->owner->ExifRead = true;
89 2
                $this->owner->Orientation = $this->owner->Image()->getOrientation();
90 2
                $this->owner->write();
91
            }
92 2
            catch (Exception $e) {
93
                error_log($e->getMessage());
94
            }
95 2
        }
96 15
    }
97
98 15
    public function onAfterWrite()
99
    {
100 15
        parent::onAfterWrite();
101
102 15
        if (!($this->owner->ExifRead)) {
103 15
            $this->processExifData();
104 15
        }
105 15
    }
106
107 1
    public function requireDefaultRecords()
108
    {
109 1
        parent::requireDefaultRecords();
110 1
        $imagesToProcess = GalleryImage::get()->filter('ExifRead', 0);
111 1
        foreach ($imagesToProcess as $image) {
112 1
            $image->processExifData();
113 1
        }
114
115 1
        DB::alteration_message('Updated image metadata where EXIF has not been ' . 'processed', 'changed');
116 1
    }
117
118
}
119