Total Complexity | 40 |
Total Lines | 267 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 0 | Features | 0 |
Complex classes like Reasons often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Reasons, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class Reasons extends Plugin |
||
47 | { |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | public $schemaVersion = '2.1.1'; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | public $hasCpSettings = false; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | public $hasCpSection = false; |
||
63 | |||
64 | // Public Methods |
||
65 | // ========================================================================= |
||
66 | |||
67 | /** |
||
68 | * @inheritdoc |
||
69 | */ |
||
70 | public function init() |
||
71 | { |
||
72 | parent::init(); |
||
73 | |||
74 | $this->setComponents([ |
||
75 | 'reasons' => ReasonsService::class, |
||
76 | ]); |
||
77 | |||
78 | Event::on( |
||
79 | Plugins::class, |
||
80 | Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
||
81 | function (PluginEvent $event) { |
||
82 | if ($event->plugin === $this) { |
||
83 | $this->reasons->clearCache(); |
||
84 | } |
||
85 | } |
||
86 | ); |
||
87 | |||
88 | Event::on( |
||
89 | Plugins::class, |
||
90 | Plugins::EVENT_AFTER_UNINSTALL_PLUGIN, |
||
91 | function (PluginEvent $event) { |
||
92 | if ($event->plugin === $this) { |
||
93 | $this->reasons->clearCache(); |
||
94 | Craft::$app->getProjectConfig()->remove('reasons_conditionals'); |
||
95 | } |
||
96 | } |
||
97 | ); |
||
98 | |||
99 | // Save or delete conditionals when field layout is saved |
||
100 | Event::on( |
||
101 | Fields::class, |
||
102 | Fields::EVENT_AFTER_SAVE_FIELD_LAYOUT, |
||
103 | function (FieldLayoutEvent $event) { |
||
104 | if (Craft::$app->getRequest()->getIsConsoleRequest()) { |
||
105 | return; |
||
106 | } |
||
107 | $fieldLayout = $event->layout; |
||
108 | if (!$fieldLayout) { |
||
109 | return; |
||
110 | } |
||
111 | $conditionals = Craft::$app->getRequest()->getBodyParam('_reasons', null); |
||
112 | if ($conditionals === null) { |
||
113 | return; |
||
114 | } |
||
115 | try { |
||
116 | if ($conditionals) { |
||
117 | $this->reasons->saveFieldLayoutConditionals($fieldLayout, $conditionals); |
||
118 | } else { |
||
119 | $this->reasons->deleteFieldLayoutConditionals($fieldLayout); |
||
120 | } |
||
121 | } catch (\Throwable $e) { |
||
122 | Craft::error($e->getMessage(), __METHOD__); |
||
123 | if (Craft::$app->getConfig()->getGeneral()->devMode) { |
||
124 | throw $e; |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | ); |
||
129 | |||
130 | // Delete conditionals when field layouts are deleted |
||
131 | Event::on( |
||
132 | Fields::class, |
||
133 | Fields::EVENT_AFTER_DELETE_FIELD_LAYOUT, |
||
134 | function (FieldLayoutEvent $event) { |
||
135 | $fieldLayout = $event->layout; |
||
136 | if (!$fieldLayout) { |
||
137 | return; |
||
138 | } |
||
139 | $this->reasons->deleteFieldLayoutConditionals($fieldLayout); |
||
140 | } |
||
141 | ); |
||
142 | |||
143 | // Clear data caches when fields are saved |
||
144 | Event::on( |
||
145 | Fields::class, |
||
146 | Fields::EVENT_AFTER_SAVE_FIELD, |
||
147 | [$this->reasons, 'clearCache'] |
||
148 | ); |
||
149 | |||
150 | // Clear data caches when fields are deleted |
||
151 | Event::on( |
||
152 | Fields::class, |
||
153 | Fields::EVENT_AFTER_DELETE_FIELD, |
||
154 | [$this->reasons, 'clearCache'] |
||
155 | ); |
||
156 | |||
157 | // Support Project Config rebuild |
||
158 | Event::on( |
||
159 | ProjectConfig::class, |
||
160 | ProjectConfig::EVENT_REBUILD, |
||
161 | [$this->reasons, 'onProjectConfigRebuild'] |
||
162 | ); |
||
163 | |||
164 | // Listen for appropriate Project Config changes |
||
165 | Craft::$app->getProjectConfig() |
||
166 | ->onAdd('reasons_conditionals.{uid}', [$this->reasons, 'onProjectConfigChange']) |
||
167 | ->onUpdate('reasons_conditionals.{uid}', [$this->reasons, 'onProjectConfigChange']) |
||
168 | ->onRemove('reasons_conditionals.{uid}', [$this->reasons, 'onProjectConfigDelete']); |
||
169 | |||
170 | // Queue up asset bundle or handle AJAX action requests |
||
171 | Event::on( |
||
172 | Application::class, |
||
173 | Application::EVENT_INIT, |
||
174 | [$this, 'initReasons'] |
||
175 | ); |
||
176 | |||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @return void |
||
181 | */ |
||
182 | public function initReasons() |
||
183 | { |
||
184 | |||
185 | $request = Craft::$app->getRequest(); |
||
186 | |||
187 | /** @var User $user */ |
||
188 | $user = Craft::$app->getUser()->getIdentity(); |
||
189 | |||
190 | if ($request->getIsConsoleRequest() || !$request->getIsCpRequest() || $request->getIsSiteRequest() || $request->getIsLoginRequest() || !$user || !$user->can('accessCp')) { |
||
191 | return; |
||
192 | } |
||
193 | |||
194 | $isAjax = $request->getIsAjax() || $request->getAcceptsJson(); |
||
195 | if ($isAjax) { |
||
196 | $this->initAjaxRequest(); |
||
197 | } else { |
||
198 | $this->registerAssetBundle(); |
||
199 | } |
||
200 | |||
201 | } |
||
202 | |||
203 | // Protected Methods |
||
204 | // ========================================================================= |
||
205 | |||
206 | /** |
||
207 | * @return void |
||
208 | */ |
||
209 | protected function registerAssetBundle() |
||
222 | ); |
||
223 | } |
||
224 | } |
||
225 | ); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @return void |
||
230 | */ |
||
231 | protected function initAjaxRequest() |
||
319 |