Completed
Push — offset ( a65dfc )
by mw
03:12
created

ExifDataAnnotator::addAnnotation()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 5
eloc 12
c 4
b 1
f 0
nc 4
nop 0
dl 0
loc 25
ccs 12
cts 12
cp 1
crap 5
rs 8.439
1
<?php
2
3
namespace SESP\Annotator;
4
5
use SESP\PropertyRegistry;
6
7
use SMW\SemanticData;
8
use SMW\DIProperty;
9
use SMW\Subobject;
10
11
use SMWDataItem as DataItem;
12
use SMWDITime as DITime;
13
use SMWDIBlob as DIBlob;
14
use SMWDINumber as DINumber;
15
16
use FormatMetadata;
17
use Title;
18
use File;
19
20
use RuntimeException;
21
22
/**
23
 * @ingroup SESP
24
 *
25
 * @license GNU GPL v2+
26
 * @since 1.0
27
 *
28
 * @author mwjames
29
 * @author rotsee
30
 * @author Stephan Gambke
31
 */
32
class ExifDataAnnotator {
33
34
	/**
35
	 * @var SemanticData
36
	 */
37
	private $semanticData = null;
38
39
	/**
40
	 * @var File
41
	 */
42
	private $file = null;
43
44
	/**
45
	 * @var Subobject
46
	 */
47
	private $subobject = null;
48
49
	/**
50
	 * @since 1.0
51
	 *
52
	 * @param SemanticData $semanticData
53
	 * @param File $file
54
	 */
55 11
	public function __construct( SemanticData $semanticData, File $file ) {
56 11
		$this->semanticData = $semanticData;
57 11
		$this->file = $file;
58 11
	}
59
60
	/**
61
	 * @since 1.0
62
	 *
63
	 * @return SemanticData
64
	 */
65 7
	public function getSemanticData() {
66 7
		return $this->semanticData;
67
	}
68
69
	/**
70
	 * @since 1.0
71
	 *
72
	 * @return boolean
73
	 */
74 10
	public function addAnnotation() {
75
76 10
		if ( !$this->file->exists() ) {
77 1
			return false;
78
		}
79
80
		// #66
81 9
		$meta = $this->file->getMetadata();
82
83 9
		if ( !$meta ) {
84 1
			return false;
85
		}
86
87
		// Guard against "Error at offset 0 of 1 bytes"
88 8
		$exif = @unserialize( $meta );
89
90 8
		if ( !is_array( $exif ) || count( $exif ) === 0 ) {
91 1
			return false;
92
		}
93
94 7
		$exif[ 'ImageWidth' ]  = $this->file->getWidth();
95 7
		$exif[ 'ImageLength' ] = $this->file->getHeight();
96
97 7
		return $this->processExifData( $exif );
98
	}
99
100 7
	protected function processExifData( $rawExif ) {
101
102 7
		$this->subobject = new Subobject( $this->getSemanticData()->getSubject()->getTitle() );
0 ignored issues
show
Bug introduced by
It seems like $this->getSemanticData()...etSubject()->getTitle() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
103 7
		$this->subobject->setSemanticData( '_EXIFDATA' );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\Subobject::setSemanticData() has been deprecated with message: since 2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
104
105 7
		$this->addPropertyValuesFromExifData( $rawExif );
106
107 7
		if ( $this->subobject->getSemanticData()->isEmpty() ) {
108 2
			return true;
109
		}
110
111 5
		$this->getSemanticData()->addPropertyObjectValue(
112 5
			new DIProperty( PropertyRegistry::getInstance()->getPropertyId( '_EXIFDATA' ) ),
113 5
			$this->subobject->getContainer()
114 5
		);
115
116 5
		return true;
117
	}
118
119 7
	protected function addPropertyValuesFromExifData( $rawExif ) {
120
121 7
		$formattedExif = FormatMetadata::getFormattedData( $rawExif );
122
123 7
		foreach ( $formattedExif as $key => $value ) {
124
125 7
			$dataItem = null;
126 7
			$propertyId = PropertyRegistry::getInstance()->getPropertyId( $key );
127
128 7
			if ( $propertyId === null ) {
129 2
				continue;
130
			}
131
132 7
			$dataItemType = PropertyRegistry::getInstance()->getPropertyType( $key );
133
134
			switch ( $dataItemType ) {
135 7
				case DataItem::TYPE_NUMBER :
136 7
					$dataItem = is_numeric( $rawExif[$key] ) ? new DINumber( $rawExif[$key] ) : null;
137 7
					break;
138 5
				case DataItem::TYPE_BLOB :
139 1
					$dataItem = new DIBlob( $value );
140 1
					break;
141 4
				case DataItem::TYPE_TIME :
142 4
					$dataItem = $this->makeDataItemTime( $rawExif[$key] );
143 4
			}
144
145 7
			if ( $dataItem !== null ) {
146 5
				$this->subobject->getSemanticData()->addPropertyObjectValue(
147 5
					new DIProperty( $propertyId ),
148
					$dataItem
149 5
				);
150 5
			}
151
152 7
		}
153 7
	}
154
155 4
	protected function makeDataItemTime( $exifValue ) {
156 4
		$datetime = $this->convertExifDate( $exifValue );
157
158 4
		if ( $datetime ) {
159 3
			return new DITime(
160 3
				DITime::CM_GREGORIAN,
161 3
				$datetime->format('Y'),
162 3
				$datetime->format('n'),
0 ignored issues
show
Documentation introduced by
$datetime->format('n') is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
163 3
				$datetime->format('j'),
0 ignored issues
show
Documentation introduced by
$datetime->format('j') is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
164 3
				$datetime->format('G'),
0 ignored issues
show
Documentation introduced by
$datetime->format('G') is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165 3
				$datetime->format('i')
0 ignored issues
show
Documentation introduced by
$datetime->format('i') is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
166 3
			);
167
		}
168 1
	}
169
170 4
	protected function convertExifDate( $exifString ) {
171
172
		// Unknown date
173 4
		if ( $exifString == '0000:00:00 00:00:00' || $exifString == '    :  :     :  :  ' ) {
174 1
			return false;
175
		}
176
177
		// Full date
178 3
		if ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $exifString ) ) {
179 1
			return new \DateTime( $exifString );
180
		}
181
182
		// No second field, timeanddate doesn't include seconds but second still available in api
183 2
		if ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $exifString ) ) {
184 1
			return new \DateTime( $exifString . ':00' );
185
		}
186
187
		// Only the date but not the time
188 1
		if (  preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $exifString ) ) {
189 1
			return new \DateTime(
190 1
				substr( $exifString, 0, 4 ) . ':' .
191 1
				substr( $exifString, 5, 2 ) . ':' .
192 1
				substr( $exifString, 8, 2 ) . ' 00:00:00'
193 1
			);
194
		}
195
196
		return false;
197
	}
198
199
}
200