Completed
Push — master ( cff413...7f9d4c )
by Peter
07:00
created

UnknownDocumentTypePanicker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 34
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A tryHandle() 0 31 5
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: peter
5
 * Date: 22.10.18
6
 * Time: 21:28
7
 */
8
9
namespace Maslosoft\Mangan\Helpers;
10
11
use Maslosoft\Mangan\Events\Event;
12
use Maslosoft\Mangan\Events\UnknownDocumentType;
13
use Maslosoft\Mangan\Exceptions\TransformatorException;
14
15
/**
16
 * This class will try to do something on unknown documents
17
 * or will panic.
18
 *
19
 * @package Maslosoft\Mangan\Helpers
20
 */
21
class UnknownDocumentTypePanicker
22
{
23
	public static function tryHandle(&$data, $parent, $parentField)
24
	{
25
		$className = '';
26
		$handled = false;
27
		if(!empty($parent))
28
		{
29
			$event = new UnknownDocumentType();
30
			$event->setData($data);
31
			$event->parent = $parent;
32
			$event->field = $parentField;
33
34
			$handled = Event::handled($parent, UnknownDocumentType::EventName, $event);
35
			if($handled)
36
			{
37
				$data = $event->getData();
38
				if(empty($data['_class']))
39
				{
40
					$handled = false;
41
				}
42
				else
43
				{
44
					$className = $data['_class'];
45
				}
46
			}
47
		}
48
		if(!$handled)
0 ignored issues
show
Bug Best Practice introduced by
The expression $handled of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
49
		{
50
			throw new TransformatorException('Could not determine document type');
51
		}
52
		return $className;
53
	}
54
}