| Total Complexity | 60 |
| Total Lines | 259 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PluginManager 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 PluginManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class PluginManager { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var ServerContainer |
||
| 47 | */ |
||
| 48 | private $container; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var IAppManager |
||
| 52 | */ |
||
| 53 | private $appManager; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * App plugins |
||
| 57 | * |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $plugins = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * App collections |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $collections = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Address book plugins |
||
| 71 | * |
||
| 72 | * @var IAddressBookProvider[]|null |
||
| 73 | */ |
||
| 74 | private $addressBookPlugins = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Calendar plugins |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private $calendarPlugins = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Contstruct a PluginManager |
||
| 85 | * |
||
| 86 | * @param ServerContainer $container server container for resolving plugin classes |
||
| 87 | * @param IAppManager $appManager app manager to loading apps and their info |
||
| 88 | */ |
||
| 89 | public function __construct(ServerContainer $container, IAppManager $appManager) { |
||
| 90 | $this->container = $container; |
||
| 91 | $this->appManager = $appManager; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Returns an array of app-registered plugins |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public function getAppPlugins() { |
||
| 100 | if (null === $this->plugins) { |
||
| 101 | $this->populate(); |
||
| 102 | } |
||
| 103 | return $this->plugins; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns an array of app-registered collections |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | public function getAppCollections() { |
||
| 112 | if (null === $this->collections) { |
||
| 113 | $this->populate(); |
||
| 114 | } |
||
| 115 | return $this->collections; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return IAddressBookProvider[] |
||
| 120 | */ |
||
| 121 | public function getAddressBookPlugins(): array { |
||
| 122 | if ($this->addressBookPlugins === null) { |
||
| 123 | $this->populate(); |
||
| 124 | } |
||
| 125 | return $this->addressBookPlugins; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns an array of app-registered calendar plugins |
||
| 130 | * |
||
| 131 | * @return array |
||
| 132 | */ |
||
| 133 | public function getCalendarPlugins():array { |
||
| 134 | if (null === $this->calendarPlugins) { |
||
| 135 | $this->populate(); |
||
| 136 | } |
||
| 137 | return $this->calendarPlugins; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Retrieve plugin and collection list and populate attributes |
||
| 142 | */ |
||
| 143 | private function populate() { |
||
| 144 | $this->plugins = []; |
||
| 145 | $this->addressBookPlugins = []; |
||
| 146 | $this->calendarPlugins = []; |
||
| 147 | $this->collections = []; |
||
| 148 | foreach ($this->appManager->getInstalledApps() as $app) { |
||
| 149 | // load plugins and collections from info.xml |
||
| 150 | $info = $this->appManager->getAppInfo($app); |
||
| 151 | if (!isset($info['types']) || !in_array('dav', $info['types'], true)) { |
||
| 152 | continue; |
||
| 153 | } |
||
| 154 | $this->loadSabrePluginsFromInfoXml($this->extractPluginList($info)); |
||
| 155 | $this->loadSabreCollectionsFromInfoXml($this->extractCollectionList($info)); |
||
| 156 | $this->loadSabreAddressBookPluginsFromInfoXml($this->extractAddressBookPluginList($info)); |
||
| 157 | $this->loadSabreCalendarPluginsFromInfoXml($this->extractCalendarPluginList($info)); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | private function extractPluginList(array $array) { |
||
| 162 | if (isset($array['sabre']) && is_array($array['sabre'])) { |
||
| 163 | if (isset($array['sabre']['plugins']) && is_array($array['sabre']['plugins'])) { |
||
| 164 | if (isset($array['sabre']['plugins']['plugin'])) { |
||
| 165 | $items = $array['sabre']['plugins']['plugin']; |
||
| 166 | if (!is_array($items)) { |
||
| 167 | $items = [$items]; |
||
| 168 | } |
||
| 169 | return $items; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | return []; |
||
| 174 | } |
||
| 175 | |||
| 176 | private function extractCollectionList(array $array) { |
||
| 177 | if (isset($array['sabre']) && is_array($array['sabre'])) { |
||
| 178 | if (isset($array['sabre']['collections']) && is_array($array['sabre']['collections'])) { |
||
| 179 | if (isset($array['sabre']['collections']['collection'])) { |
||
| 180 | $items = $array['sabre']['collections']['collection']; |
||
| 181 | if (!is_array($items)) { |
||
| 182 | $items = [$items]; |
||
| 183 | } |
||
| 184 | return $items; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | return []; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param array $array |
||
| 193 | * |
||
| 194 | * @return string[] |
||
| 195 | */ |
||
| 196 | private function extractAddressBookPluginList(array $array): array { |
||
| 197 | if (!isset($array['sabre']) || !is_array($array['sabre'])) { |
||
| 198 | return []; |
||
| 199 | } |
||
| 200 | if (!isset($array['sabre']['address-book-plugins']) || !is_array($array['sabre']['address-book-plugins'])) { |
||
| 201 | return []; |
||
| 202 | } |
||
| 203 | if (!isset($array['sabre']['address-book-plugins']['plugin'])) { |
||
| 204 | return []; |
||
| 205 | } |
||
| 206 | |||
| 207 | $items = $array['sabre']['address-book-plugins']['plugin']; |
||
| 208 | if (!is_array($items)) { |
||
| 209 | $items = [$items]; |
||
| 210 | } |
||
| 211 | return $items; |
||
| 212 | } |
||
| 213 | |||
| 214 | private function extractCalendarPluginList(array $array):array { |
||
| 215 | if (isset($array['sabre']) && is_array($array['sabre'])) { |
||
| 216 | if (isset($array['sabre']['calendar-plugins']) && is_array($array['sabre']['calendar-plugins'])) { |
||
| 217 | if (isset($array['sabre']['calendar-plugins']['plugin'])) { |
||
| 218 | $items = $array['sabre']['calendar-plugins']['plugin']; |
||
| 219 | if (!is_array($items)) { |
||
| 220 | $items = [$items]; |
||
| 221 | } |
||
| 222 | return $items; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | return []; |
||
| 227 | } |
||
| 228 | |||
| 229 | private function loadSabrePluginsFromInfoXml(array $plugins) { |
||
| 230 | foreach ($plugins as $plugin) { |
||
| 231 | try { |
||
| 232 | $this->plugins[] = $this->container->query($plugin); |
||
| 233 | } catch (QueryException $e) { |
||
| 234 | if (class_exists($plugin)) { |
||
| 235 | $this->plugins[] = new $plugin(); |
||
| 236 | } else { |
||
| 237 | throw new \Exception("Sabre plugin class '$plugin' is unknown and could not be loaded"); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | private function loadSabreCollectionsFromInfoXml(array $collections) { |
||
| 244 | foreach ($collections as $collection) { |
||
| 245 | try { |
||
| 246 | $this->collections[] = $this->container->query($collection); |
||
| 247 | } catch (QueryException $e) { |
||
| 248 | if (class_exists($collection)) { |
||
| 249 | $this->collections[] = new $collection(); |
||
| 250 | } else { |
||
| 251 | throw new \Exception("Sabre collection class '$collection' is unknown and could not be loaded"); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | private function createPluginInstance(string $className) { |
||
| 258 | try { |
||
| 259 | return $this->container->query($className); |
||
| 260 | } catch (QueryException $e) { |
||
| 261 | if (class_exists($className)) { |
||
| 262 | return new $className(); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | throw new \Exception("Sabre plugin class '$className' is unknown and could not be loaded"); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param string[] $plugin |
||
| 271 | */ |
||
| 272 | private function loadSabreAddressBookPluginsFromInfoXml(array $plugins): void { |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | private function loadSabreCalendarPluginsFromInfoXml(array $calendarPlugins):void { |
||
| 286 | foreach ($calendarPlugins as $calendarPlugin) { |
||
| 287 | try { |
||
| 288 | $instantiatedCalendarPlugin = $this->container->query($calendarPlugin); |
||
| 289 | } catch (QueryException $e) { |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | } |
||
| 306 |