1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Gedmo\Sortable\Mapping\Driver\Fluent as FluentDriver; |
6
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
7
|
|
|
use LaravelDoctrine\Fluent\Builders\Field; |
8
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
9
|
|
|
use LaravelDoctrine\Fluent\Relations\ManyToMany; |
10
|
|
|
use LaravelDoctrine\Fluent\Relations\ManyToOne; |
11
|
|
|
|
12
|
|
|
class SortableGroup implements Buildable |
13
|
|
|
{ |
14
|
|
|
const MACRO_METHOD = 'sortableGroup'; |
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
|
|
|
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName) |
31
|
|
|
{ |
32
|
|
|
$this->classMetadata = $classMetadata; |
33
|
|
|
$this->fieldName = $fieldName; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return the name of the actual extension. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getExtensionName() |
42
|
|
|
{ |
43
|
|
|
return FluentDriver::EXTENSION_NAME; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public static function enable() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
Field::macro(self::MACRO_METHOD, function (Field $builder) { |
52
|
|
|
return new static($builder->getClassMetadata(), $builder->getName()); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
ManyToOne::macro(self::MACRO_METHOD, function (ManyToOne $builder) { |
56
|
|
|
return new static($builder->getClassMetadata(), $builder->getRelation()); |
|
|
|
|
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
ManyToMany::macro(self::MACRO_METHOD, function (ManyToMany $builder) { |
60
|
|
|
return new static($builder->getClassMetadata(), $builder->getRelation()); |
|
|
|
|
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Execute the build process |
66
|
|
|
*/ |
67
|
|
|
public function build() |
68
|
|
|
{ |
69
|
|
|
$this->classMetadata->appendExtension($this->getExtensionName(), [ |
70
|
|
|
'groups' => [ |
71
|
|
|
$this->fieldName |
72
|
|
|
] |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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.