Completed
Push — master ( 386860...f580c3 )
by Peter
06:34
created

Datamatrix   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 25%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 41
ccs 3
cts 12
cp 0.25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromModel() 0 10 2
A toModel() 0 10 2
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Transformers;
15
16
use Dmtx\Reader;
17
use Dmtx\Writer;
18
use Maslosoft\Cli\Shared\Os;
19
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
20
use Maslosoft\Addendum\Utilities\ClassChecker;
21
use Maslosoft\Mangan\Exceptions\ManganException;
22
use Maslosoft\Mangan\Exceptions\TransformatorException;
23
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
24
25
/**
26
 * Datamatrix
27
 *
28
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
29
 */
30
class Datamatrix implements TransformatorInterface
31
{
32
33
	/**
34
	 * Returns the given object as an associative array
35
	 * @param AnnotatedInterface|object $model
36
	 * @param string[] $fields Fields to transform
37
	 * @return array an associative array of the contents of this object
38
	 */
39 1
	public static function fromModel(AnnotatedInterface $model, $fields = [])
40
	{
41 1
		if(!ClassChecker::exists(Writer::class))
42
		{
43 1
			throw new ManganException('Missing php-dmtx library');
44
		}
45
		assert(Os::commandExists('dmtxwrite'));
0 ignored issues
show
Bug introduced by
The method commandExists() does not seem to exist on object<Maslosoft\Cli\Shared\Os>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
		$data = YamlString::fromModel($model, $fields, 1, 1);
47
		return (new Writer())->encode($data)->dump();
48
	}
49
50
	/**
51
	 * Create document from datamatrix code
52
	 *
53
	 * @param mixed[] $data
54
	 * @param string|object $className
55
	 * @param AnnotatedInterface $instance
56
	 * @return AnnotatedInterface
57
	 * @throws TransformatorException
58
	 */
59
	public static function toModel($data, $className = null, AnnotatedInterface $instance = null)
60
	{
61
		if(!ClassChecker::exists(Reader::class))
62
		{
63
			throw new ManganException('Missing php-dmtx library');
64
		}
65
		assert(Os::commandExists('dmtxread'));
0 ignored issues
show
Bug introduced by
The method commandExists() does not seem to exist on object<Maslosoft\Cli\Shared\Os>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
		$data = (new Reader())->decode($data);
67
		return YamlString::toModel($data, $className, $instance);
68
	}
69
70
}
71