1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Gedmo\Exception\InvalidMappingException; |
6
|
|
|
use Gedmo\Tree\Mapping\Driver\Fluent as FluentDriver; |
7
|
|
|
use Gedmo\Tree\Mapping\Validator; |
8
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
9
|
|
|
use LaravelDoctrine\Fluent\Builders\Field; |
10
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
11
|
|
|
|
12
|
|
View Code Duplication |
class TreePathSource implements Buildable |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
const MACRO_METHOD = 'treePathSource'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ExtensibleClassMetadata |
18
|
|
|
*/ |
19
|
|
|
protected $classMetadata; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $fieldName; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
28
|
|
|
* @param string $fieldName |
29
|
|
|
*/ |
30
|
3 |
|
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName) |
31
|
|
|
{ |
32
|
3 |
|
$this->classMetadata = $classMetadata; |
33
|
3 |
|
$this->fieldName = $fieldName; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Enable TreePathSource |
38
|
|
|
*/ |
39
|
|
|
public static function enable() |
40
|
|
|
{ |
41
|
2 |
|
Field::macro(self::MACRO_METHOD, function (Field $field) { |
42
|
1 |
|
return new static($field->getClassMetadata(), $field->getName()); |
|
|
|
|
43
|
2 |
|
}); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Execute the build process |
48
|
|
|
*/ |
49
|
2 |
|
public function build() |
50
|
|
|
{ |
51
|
2 |
|
if (!(new Validator)->isValidFieldForPathSource($this->classMetadata, $this->fieldName)) { |
52
|
1 |
|
throw new InvalidMappingException( |
53
|
1 |
|
"Tree PathSource field - [{$this->fieldName}] type is not valid. It can be any of the integer variants, double, float or string in class - {$this->classMetadata->name}" |
54
|
1 |
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$this->classMetadata->appendExtension($this->getExtensionName(), [ |
58
|
1 |
|
'path_source' => $this->fieldName |
59
|
1 |
|
]); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return the name of the actual extension. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
1 |
|
public function getExtensionName() |
68
|
|
|
{ |
69
|
1 |
|
return FluentDriver::EXTENSION_NAME; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.