| Conditions | 4 |
| Paths | 4 |
| Total Lines | 74 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 43 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 44 | { |
||
| 45 | $io = new SymfonyStyle($input, $output); |
||
| 46 | |||
| 47 | $io->title('Generate Missing Subject Identifiers'); |
||
| 48 | |||
| 49 | $this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); |
||
| 50 | |||
| 51 | $io->section("Searching Authorizations with missing Subject Identifiers..."); |
||
| 52 | |||
| 53 | $repo = $this->getAuthorizationRepository(); |
||
| 54 | $count = $repo->createQueryBuilder('a') |
||
| 55 | ->select('COUNT(a)') |
||
| 56 | ->leftJoin( |
||
| 57 | 'LoginCidadaoOpenIDBundle:SubjectIdentifier', |
||
| 58 | 's', |
||
| 59 | 'WITH', |
||
| 60 | 's.person = a.person AND s.client = a.client' |
||
| 61 | ) |
||
| 62 | ->where('s.subjectIdentifier IS NULL') |
||
| 63 | ->getQuery() |
||
| 64 | ->getSingleScalarResult(); |
||
| 65 | |||
| 66 | if ($count === 0) { |
||
| 67 | $io->success("No changes needed. You're all set!"); |
||
| 68 | |||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | $io->text("{$count} entries found"); |
||
| 73 | |||
| 74 | $missingSubId = $repo->createQueryBuilder('a') |
||
| 75 | ->leftJoin( |
||
| 76 | 'LoginCidadaoOpenIDBundle:SubjectIdentifier', |
||
| 77 | 's', |
||
| 78 | 'WITH', |
||
| 79 | 's.person = a.person AND s.client = a.client' |
||
| 80 | ) |
||
| 81 | ->where('s.subjectIdentifier IS NULL') |
||
| 82 | ->getQuery()->iterate(); |
||
| 83 | |||
| 84 | $io->section("Generating Sector Identifiers..."); |
||
| 85 | |||
| 86 | /** @var SubjectIdentifierService $subIdService */ |
||
| 87 | $subIdService = $this->getContainer()->get('oidc.subject_identifier.service'); |
||
| 88 | |||
| 89 | $io->progressStart($count); |
||
| 90 | $current = 0; |
||
| 91 | foreach ($missingSubId as $row) { |
||
| 92 | /** @var Authorization $auth */ |
||
| 93 | $auth = $row[0]; |
||
| 94 | $subId = $subIdService->getSubjectIdentifier( |
||
| 95 | $auth->getPerson(), |
||
|
|
|||
| 96 | $this->getClientMetadata($auth->getClient()), |
||
| 97 | false |
||
| 98 | ); |
||
| 99 | $sub = new SubjectIdentifier(); |
||
| 100 | $sub |
||
| 101 | ->setPerson($auth->getPerson()) |
||
| 102 | ->setClient($auth->getClient()) |
||
| 103 | ->setSubjectIdentifier($subId); |
||
| 104 | $this->em->persist($sub); |
||
| 105 | if ($current++ % 50 === 0) { |
||
| 106 | $this->em->flush(); |
||
| 107 | $this->em->clear(); |
||
| 108 | } |
||
| 109 | $io->progressAdvance(); |
||
| 110 | } |
||
| 111 | $this->em->flush(); |
||
| 112 | $this->em->clear(); |
||
| 113 | $io->progressFinish(); |
||
| 114 | |||
| 115 | $io->success("Done! {$count} Authorizations updated!"); |
||
| 116 | } |
||
| 117 | |||
| 146 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: