Completed
Push — master ( e0f8d3...c7a6bb )
by Peter
16:49
created

SecretDecorator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 43
ccs 12
cts 14
cp 0.8571
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 5 1
A write() 0 16 4
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace Maslosoft\Mangan\Decorators\Property;
10
11
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
12
use Maslosoft\Mangan\Exceptions\ManganException;
13
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
14
use Maslosoft\Mangan\Interfaces\InternationalInterface;
15
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
16
use Maslosoft\Mangan\Meta\ManganMeta;
17
18
/**
19
 * SecretDecorator
20
 *
21
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
22
 */
23
class SecretDecorator implements DecoratorInterface
24
{
25
26
	/**
27
	 * This will be called when getting value.
28
	 * This should return end user value.
29
	 * @param AnnotatedInterface $model Document model which will be decorated
30
	 * @param string $name Field name
31
	 * @param mixed $dbValue
32
	 * @return bool Return true if value should be assigned to model
33
	 */
34 2
	public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
35
	{
36 2
		$model->$name = $dbValue;
37 2
		return true;
38
	}
39
40
	/**
41
	 * This will be called when setting value.
42
	 * This should return db acceptable value
43
	 * @param AnnotatedInterface $model Document model which will be decorated
44
	 * @param string $name Field name
45
	 * @param mixed $dbValue
46
	 * @return bool Return true to store value to database
47
	 */
48 2
	public function write($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
49
	{
50 2
		$secretMeta = ManganMeta::create($model)->field($name)->secret;
51 2
		if (false === $secretMeta)
52 2
		{
53
			return true;
54
		}
55 2
		if (empty($secretMeta->callback) || !$secretMeta->callback)
56 2
		{
57
			return true;
58
		}
59 2
		$converted = call_user_func($secretMeta->callback, $model->$name);
60 2
		$dbValue[$name] = $converted;
61
62 2
		return true;
63
	}
64
65
}
66