| Conditions | 4 |
| Paths | 4 |
| Total Lines | 62 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 28 | public function up(Schema $schema): void |
||
| 29 | { |
||
| 30 | $container = $this->getContainer(); |
||
| 31 | $em = $this->getEntityManager(); |
||
| 32 | |||
| 33 | /** @var Connection $connection */ |
||
| 34 | $connection = $em->getConnection(); |
||
| 35 | |||
| 36 | $kernel = $container->get('kernel'); |
||
| 37 | $rootPath = $kernel->getProjectDir(); |
||
| 38 | |||
| 39 | $repo = $container->get(SocialPostAttachmentRepository::class); |
||
| 40 | $userRepo = $container->get(UserRepository::class); |
||
| 41 | $admin = $this->getAdmin(); |
||
| 42 | |||
| 43 | $sub = $em->createQueryBuilder(); |
||
| 44 | $sub->select('sp.id') |
||
| 45 | ->from('Chamilo\CoreBundle\Entity\SocialPost', 'sp') |
||
| 46 | ; |
||
| 47 | |||
| 48 | $qb = $em->createQueryBuilder(); |
||
| 49 | $qb->select('ma') |
||
| 50 | ->from('Chamilo\CoreBundle\Entity\MessageAttachment', 'ma') |
||
| 51 | ->where($qb->expr()->in('ma.message', $sub->getDQL())) |
||
| 52 | ; |
||
| 53 | |||
| 54 | $query = $qb->getQuery(); |
||
| 55 | $messageAttachments = $query->getResult(); |
||
| 56 | |||
| 57 | foreach ($messageAttachments as $attachment) { |
||
| 58 | $message = $attachment->getMessage(); |
||
| 59 | if ($message) { |
||
| 60 | $messageId = $message->getId(); |
||
| 61 | $filename = $attachment->getFilename(); |
||
| 62 | $rootDir = $rootPath.'/app/upload/users'; |
||
| 63 | $targetFile = $attachment->getPath(); |
||
| 64 | $foundFilePath = $this->findFileRecursively($rootDir, $targetFile); |
||
| 65 | $sender = $message->getSender(); |
||
| 66 | |||
| 67 | if ($foundFilePath) { |
||
| 68 | error_log("File found in $foundFilePath"); |
||
| 69 | |||
| 70 | $mimeType = mime_content_type($foundFilePath); |
||
| 71 | $uploadFile = new UploadedFile($foundFilePath, $filename, $mimeType, null, true); |
||
| 72 | |||
| 73 | $socialPost = $em->getRepository(SocialPost::class)->find($messageId); |
||
| 74 | |||
| 75 | $attachment = new SocialPostAttachment(); |
||
| 76 | $attachment->setSocialPost($socialPost); |
||
| 77 | $attachment->setPath(uniqid('social_post', true)); |
||
| 78 | $attachment->setFilename($uploadFile->getClientOriginalName()); |
||
| 79 | $attachment->setSize($uploadFile->getSize()); |
||
| 80 | $attachment->setInsertUserId($sender->getId()); |
||
| 81 | $attachment->setInsertDateTime(new DateTime('now', new DateTimeZone('UTC'))); |
||
| 82 | $attachment->setParent($sender); |
||
| 83 | $attachment->addUserLink($sender); |
||
| 84 | $attachment->setCreator($sender); |
||
| 85 | |||
| 86 | $em->persist($attachment); |
||
| 87 | $em->flush(); |
||
| 88 | |||
| 89 | $repo->addFile($attachment, $uploadFile); |
||
| 90 | } |
||
| 123 |