| Conditions | 7 |
| Paths | 8 |
| Total Lines | 68 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 22 | public function up(Schema $schema): void |
||
| 23 | { |
||
| 24 | $container = $this->getContainer(); |
||
| 25 | $doctrine = $container->get('doctrine'); |
||
| 26 | $em = $doctrine->getManager(); |
||
| 27 | /** @var Connection $connection */ |
||
| 28 | $connection = $em->getConnection(); |
||
| 29 | |||
| 30 | $lpItemRepo = $container->get(CLpItemRepository::class); |
||
| 31 | |||
| 32 | $batchSize = self::BATCH_SIZE; |
||
| 33 | //$q = $em->createQuery('SELECT lp FROM Chamilo\CourseBundle\Entity\CLp lp WHERE lp.iid = 263 ORDER BY lp.iid'); |
||
| 34 | $q = $em->createQuery('SELECT lp FROM Chamilo\CourseBundle\Entity\CLp lp'); |
||
| 35 | $counter = 1; |
||
| 36 | /** @var CLp $lp */ |
||
| 37 | foreach ($q->toIterable() as $lp) { |
||
| 38 | $lpId = $lp->getIid(); |
||
| 39 | error_log("LP #$lpId"); |
||
| 40 | |||
| 41 | /** @var CLp $resource */ |
||
| 42 | //$resource = $lpRepo->find($lpId); |
||
| 43 | if (!$lp->hasResourceNode()) { |
||
| 44 | error_log("no resource node"); |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | // Root item is created in the previous Migration. |
||
| 49 | $rootItem = $lpItemRepo->getRootItem($lpId); |
||
| 50 | |||
| 51 | if (null === $rootItem) { |
||
| 52 | error_log("no root item"); |
||
| 53 | continue; |
||
| 54 | } |
||
| 55 | |||
| 56 | // Migrate c_lp_item |
||
| 57 | $sql = "SELECT * FROM c_lp_item WHERE lp_id = $lpId AND path <> 'root' |
||
| 58 | ORDER BY display_order"; |
||
| 59 | $resultItems = $connection->executeQuery($sql); |
||
| 60 | $lpItems = $resultItems->fetchAllAssociative(); |
||
| 61 | |||
| 62 | if (empty($lpItems)) { |
||
| 63 | error_log("no items"); |
||
| 64 | continue; |
||
| 65 | } |
||
| 66 | |||
| 67 | $orderList = []; |
||
| 68 | foreach ($lpItems as $item) { |
||
| 69 | $object = new \stdClass(); |
||
| 70 | $object->id = $item['iid']; |
||
| 71 | $object->parent_id = (int) $item['parent_item_id']; |
||
| 72 | $orderList[] = $object; |
||
| 73 | } |
||
| 74 | |||
| 75 | echo '<pre>'; |
||
| 76 | var_dump($lpId, $lpItems); |
||
|
|
|||
| 77 | echo '</pre>'; |
||
| 78 | error_log("save new order"); |
||
| 79 | \learnpath::sortItemByOrderList($rootItem, $orderList, true); |
||
| 80 | if (($counter % $batchSize) === 0) { |
||
| 81 | $em->flush(); |
||
| 82 | $em->clear(); // Detaches all objects from Doctrine! |
||
| 83 | } |
||
| 84 | $counter++; |
||
| 85 | $em->flush(); |
||
| 86 | } |
||
| 87 | |||
| 88 | $em->flush(); |
||
| 89 | $em->clear(); |
||
| 90 | } |
||
| 92 |