Completed
Push — master ( 0e5c07...6c4975 )
by Peter
24:11
created

SecretDecorator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 5 1
B write() 0 18 5
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\Interfaces\Decorators\Property\DecoratorInterface;
13
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
14
use Maslosoft\Mangan\Meta\ManganMeta;
15
16
/**
17
 * SecretDecorator
18
 *
19
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
20
 */
21
class SecretDecorator implements DecoratorInterface
22
{
23
24
	/**
25
	 * This will be called when getting value.
26
	 * This should return end user value.
27
	 * @param AnnotatedInterface $model Document model which will be decorated
28
	 * @param string $name Field name
29
	 * @param mixed $dbValue
30
	 * @return bool Return true if value should be assigned to model
31
	 */
32
	public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
33
	{
34 3
		$model->$name = $dbValue;
35
		return true;
36 3
	}
37 3
38
	/**
39
	 * This will be called when setting value.
40
	 * This should return db acceptable value
41
	 * @param AnnotatedInterface $model Document model which will be decorated
42
	 * @param string $name Field name
43
	 * @param mixed $dbValue
44
	 * @return bool Return true to store value to database
45
	 */
46
	public function write($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
47
	{
48 2
		$secretMeta = ManganMeta::create($model)->field($name)->secret;
49
		if (false === $secretMeta)
50 2
		{
51 2
			return true;
52 2
		}
53
		if (empty($secretMeta->callback) || !$secretMeta->callback)
54
		{
55 2
			return true;
56 2
		}
57
		if(!empty($model->$name))
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