| Total Complexity | 43 |
| Total Lines | 337 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 47 | class Manager implements IManager { |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var ICalendar[] holds all registered calendars |
||
| 51 | */ |
||
| 52 | private $calendars = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \Closure[] to call to load/register calendar providers |
||
| 56 | */ |
||
| 57 | private $calendarLoaders = []; |
||
| 58 | |||
| 59 | /** @var Coordinator */ |
||
| 60 | private $coordinator; |
||
| 61 | |||
| 62 | /** @var ContainerInterface */ |
||
| 63 | private $container; |
||
| 64 | |||
| 65 | /** @var LoggerInterface */ |
||
| 66 | private $logger; |
||
| 67 | |||
| 68 | private ITimeFactory $timeFactory; |
||
| 69 | |||
| 70 | |||
| 71 | public function __construct(Coordinator $coordinator, |
||
| 72 | ContainerInterface $container, |
||
| 73 | LoggerInterface $logger, |
||
| 74 | ITimeFactory $timeFactory) { |
||
| 75 | $this->coordinator = $coordinator; |
||
| 76 | $this->container = $container; |
||
| 77 | $this->logger = $logger; |
||
| 78 | $this->timeFactory = $timeFactory; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * This function is used to search and find objects within the user's calendars. |
||
| 83 | * In case $pattern is empty all events/journals/todos will be returned. |
||
| 84 | * |
||
| 85 | * @param string $pattern which should match within the $searchProperties |
||
| 86 | * @param array $searchProperties defines the properties within the query pattern should match |
||
| 87 | * @param array $options - optional parameters: |
||
| 88 | * ['timerange' => ['start' => new DateTime(...), 'end' => new DateTime(...)]] |
||
| 89 | * @param integer|null $limit - limit number of search results |
||
| 90 | * @param integer|null $offset - offset for paging of search results |
||
| 91 | * @return array an array of events/journals/todos which are arrays of arrays of key-value-pairs |
||
| 92 | * @since 13.0.0 |
||
| 93 | */ |
||
| 94 | public function search($pattern, array $searchProperties = [], array $options = [], $limit = null, $offset = null) { |
||
| 95 | $this->loadCalendars(); |
||
| 96 | $result = []; |
||
| 97 | foreach ($this->calendars as $calendar) { |
||
| 98 | $r = $calendar->search($pattern, $searchProperties, $options, $limit, $offset); |
||
| 99 | foreach ($r as $o) { |
||
| 100 | $o['calendar-key'] = $calendar->getKey(); |
||
| 101 | $result[] = $o; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return $result; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Check if calendars are available |
||
| 110 | * |
||
| 111 | * @return bool true if enabled, false if not |
||
| 112 | * @since 13.0.0 |
||
| 113 | */ |
||
| 114 | public function isEnabled() { |
||
| 115 | return !empty($this->calendars) || !empty($this->calendarLoaders); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Registers a calendar |
||
| 120 | * |
||
| 121 | * @param ICalendar $calendar |
||
| 122 | * @return void |
||
| 123 | * @since 13.0.0 |
||
| 124 | */ |
||
| 125 | public function registerCalendar(ICalendar $calendar) { |
||
| 126 | $this->calendars[$calendar->getKey()] = $calendar; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Unregisters a calendar |
||
| 131 | * |
||
| 132 | * @param ICalendar $calendar |
||
| 133 | * @return void |
||
| 134 | * @since 13.0.0 |
||
| 135 | */ |
||
| 136 | public function unregisterCalendar(ICalendar $calendar) { |
||
| 137 | unset($this->calendars[$calendar->getKey()]); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * In order to improve lazy loading a closure can be registered which will be called in case |
||
| 142 | * calendars are actually requested |
||
| 143 | * |
||
| 144 | * @param \Closure $callable |
||
| 145 | * @return void |
||
| 146 | * @since 13.0.0 |
||
| 147 | */ |
||
| 148 | public function register(\Closure $callable) { |
||
| 149 | $this->calendarLoaders[] = $callable; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return ICalendar[] |
||
| 154 | * @since 13.0.0 |
||
| 155 | */ |
||
| 156 | public function getCalendars() { |
||
| 157 | $this->loadCalendars(); |
||
| 158 | |||
| 159 | return array_values($this->calendars); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * removes all registered calendar instances |
||
| 164 | * @return void |
||
| 165 | * @since 13.0.0 |
||
| 166 | */ |
||
| 167 | public function clear() { |
||
| 168 | $this->calendars = []; |
||
| 169 | $this->calendarLoaders = []; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * loads all calendars |
||
| 174 | */ |
||
| 175 | private function loadCalendars() { |
||
| 176 | foreach ($this->calendarLoaders as $callable) { |
||
| 177 | $callable($this); |
||
| 178 | } |
||
| 179 | $this->calendarLoaders = []; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $principalUri |
||
| 184 | * @param array $calendarUris |
||
| 185 | * @return ICreateFromString[] |
||
| 186 | */ |
||
| 187 | public function getCalendarsForPrincipal(string $principalUri, array $calendarUris = []): array { |
||
| 188 | $context = $this->coordinator->getRegistrationContext(); |
||
| 189 | if ($context === null) { |
||
| 190 | return []; |
||
| 191 | } |
||
| 192 | |||
| 193 | return array_merge( |
||
| 194 | ...array_map(function ($registration) use ($principalUri, $calendarUris) { |
||
| 195 | try { |
||
| 196 | /** @var ICalendarProvider $provider */ |
||
| 197 | $provider = $this->container->get($registration->getService()); |
||
| 198 | } catch (Throwable $e) { |
||
| 199 | $this->logger->error('Could not load calendar provider ' . $registration->getService() . ': ' . $e->getMessage(), [ |
||
| 200 | 'exception' => $e, |
||
| 201 | ]); |
||
| 202 | return []; |
||
| 203 | } |
||
| 204 | |||
| 205 | return $provider->getCalendars($principalUri, $calendarUris); |
||
| 206 | }, $context->getCalendarProviders()) |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | |||
| 210 | public function searchForPrincipal(ICalendarQuery $query): array { |
||
| 233 | } |
||
| 234 | |||
| 235 | public function newQuery(string $principalUri): ICalendarQuery { |
||
| 236 | return new CalendarQuery($principalUri); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @throws \OCP\DB\Exception |
||
| 241 | */ |
||
| 242 | public function handleIMipReply(string $principalUri, string $sender, string $recipient, string $calendarData): bool { |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @since 25.0.0 |
||
| 309 | * @throws \OCP\DB\Exception |
||
| 310 | */ |
||
| 311 | public function handleIMipCancel(string $principalUri, string $sender, ?string $replyTo, string $recipient, string $calendarData): bool { |
||
| 387 |