Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
73 | class CirclesList extends Base { |
||
74 | |||
75 | |||
76 | use TArrayTools; |
||
77 | |||
78 | |||
79 | /** @var ModelManager */ |
||
80 | private $modelManager; |
||
81 | |||
82 | /** @var FederatedUserService */ |
||
83 | private $federatedUserService; |
||
84 | |||
85 | /** @var RemoteService */ |
||
86 | private $remoteService; |
||
87 | |||
88 | /** @var CircleService */ |
||
89 | private $circleService; |
||
90 | |||
91 | /** @var ConfigService */ |
||
92 | private $configService; |
||
93 | |||
94 | |||
95 | /** @var InputInterface */ |
||
96 | private $input; |
||
97 | |||
98 | |||
99 | /** |
||
100 | * CirclesList constructor. |
||
101 | * |
||
102 | * @param ModelManager $modelManager |
||
103 | * @param FederatedUserService $federatedUserService |
||
104 | * @param RemoteService $remoteService |
||
105 | * @param CircleService $circleService |
||
106 | * @param ConfigService $configService |
||
107 | */ |
||
108 | public function __construct( |
||
109 | ModelManager $modelManager, FederatedUserService $federatedUserService, RemoteService $remoteService, |
||
110 | CircleService $circleService, ConfigService $configService |
||
111 | ) { |
||
112 | parent::__construct(); |
||
113 | $this->modelManager = $modelManager; |
||
114 | $this->federatedUserService = $federatedUserService; |
||
115 | $this->remoteService = $remoteService; |
||
116 | $this->circleService = $circleService; |
||
117 | $this->configService = $configService; |
||
118 | } |
||
119 | |||
120 | |||
121 | protected function configure() { |
||
122 | parent::configure(); |
||
123 | $this->setName('circles:manage:list') |
||
124 | ->setDescription('listing current circles') |
||
125 | ->addOption('instance', '', InputOption::VALUE_REQUIRED, 'Instance of the circle', '') |
||
126 | ->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '') |
||
127 | ->addOption('initiator-type', '', InputOption::VALUE_REQUIRED, 'set initiator type', '0') |
||
128 | ->addOption('member', '', InputOption::VALUE_REQUIRED, 'search for member', '') |
||
129 | ->addOption('def', '', InputOption::VALUE_NONE, 'display complete circle configuration') |
||
130 | ->addOption('display-name', '', InputOption::VALUE_NONE, 'display the displayName') |
||
131 | ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles'); |
||
132 | } |
||
133 | |||
134 | |||
135 | /** |
||
136 | * @param InputInterface $input |
||
137 | * @param OutputInterface $output |
||
138 | * |
||
139 | * @return int |
||
140 | * @throws CircleNotFoundException |
||
141 | * @throws FederatedUserException |
||
142 | * @throws FederatedUserNotFoundException |
||
143 | * @throws InitiatorNotFoundException |
||
144 | * @throws InvalidIdException |
||
145 | * @throws OwnerNotFoundException |
||
146 | * @throws RemoteInstanceException |
||
147 | * @throws RemoteNotFoundException |
||
148 | * @throws RemoteResourceNotFoundException |
||
149 | * @throws RequestNetworkException |
||
150 | * @throws SignatoryException |
||
151 | * @throws UnknownRemoteException |
||
152 | * @throws UserTypeNotFoundException |
||
153 | * @throws FederatedItemException |
||
154 | * @throws MemberNotFoundException |
||
155 | * @throws SingleCircleNotFoundException |
||
156 | * @throws RequestBuilderException |
||
157 | */ |
||
158 | protected function execute(InputInterface $input, OutputInterface $output): int { |
||
159 | $this->input = $input; |
||
160 | $member = $input->getOption('member'); |
||
161 | $instance = $input->getOption('instance'); |
||
162 | $initiator = $input->getOption('initiator'); |
||
163 | |||
164 | $filterMember = null; |
||
165 | if ($member !== '') { |
||
166 | $filterMember = $this->federatedUserService->getFederatedMember($member); |
||
167 | } |
||
168 | |||
169 | if (!$this->configService->isLocalInstance($instance)) { |
||
170 | $data = ['filterMember' => $filterMember]; |
||
171 | if ($initiator) { |
||
172 | $data['initiator'] = $this->federatedUserService->getFederatedUser( |
||
173 | $initiator, |
||
174 | Member::parseTypeString($input->getOption('initiator-type')), |
||
175 | ); |
||
|
|||
176 | } |
||
177 | |||
178 | $circles = $this->remoteService->getCirclesFromInstance($instance, $data); |
||
179 | } else { |
||
180 | $this->federatedUserService->commandLineInitiator( |
||
181 | $initiator, |
||
182 | Member::parseTypeString($input->getOption('initiator-type')), |
||
183 | '', |
||
184 | true |
||
185 | ); |
||
186 | |||
187 | $params = new SimpleDataStore(['includeSystemCircles' => $input->getOption('all')]); |
||
188 | $circles = $this->circleService->getCircles(null, $filterMember, $params); |
||
189 | } |
||
190 | |||
191 | if (strtolower($input->getOption('output')) === 'json') { |
||
192 | $output->writeln(json_encode($circles, JSON_PRETTY_PRINT)); |
||
193 | |||
194 | return 0; |
||
195 | } |
||
196 | |||
197 | $this->displayCircles($circles); |
||
198 | |||
199 | return 0; |
||
200 | } |
||
201 | |||
202 | |||
203 | /** |
||
204 | * @param Circle[] $circles |
||
205 | */ |
||
206 | private function displayCircles(array $circles): void { |
||
207 | $output = new ConsoleOutput(); |
||
208 | $output = $output->section(); |
||
209 | $table = new Table($output); |
||
210 | $table->setHeaders( |
||
211 | ['Single Id', 'Name', 'Config', 'Source', 'Owner', 'Instance', 'Limit', 'Description'] |
||
212 | ); |
||
213 | $table->render(); |
||
214 | |||
215 | $display = ($this->input->getOption('def') ? Circle::FLAGS_LONG : Circle::FLAGS_SHORT); |
||
216 | foreach ($circles as $circle) { |
||
217 | $owner = $circle->getOwner(); |
||
218 | $table->appendRow( |
||
219 | [ |
||
220 | $circle->getSingleId(), |
||
221 | $this->input->getOption('display-name') ? $circle->getDisplayName() : $circle->getName(), |
||
222 | json_encode(Circle::getCircleFlags($circle, $display)), |
||
223 | Circle::$DEF_SOURCE[$circle->getSource()], |
||
224 | $owner->getUserId(), |
||
225 | $this->configService->displayInstance($owner->getInstance()), |
||
226 | $this->getInt('members_limit', $circle->getSettings(), -1), |
||
227 | substr(str_replace("\n", ' ', $circle->getDescription()), 0, 30) |
||
235 |