| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace OroCRM\Bundle\SalesBundle\Migrations\Schema\v1_24; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\DBAL\Schema\Schema; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\DBAL\Types\Type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\DependencyInjection\ContainerAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\DependencyInjection\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Oro\Bundle\MigrationBundle\Migration\OrderedMigrationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Oro\Bundle\MigrationBundle\Migration\Migration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Oro\Bundle\MigrationBundle\Migration\ParametrizedSqlMigrationQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Oro\Bundle\MigrationBundle\Migration\QueryBag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 16 |  | View Code Duplication | class UpdateLeadStatus implements | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     Migration, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     ContainerAwareInterface, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     OrderedMigrationInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var ContainerInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     public function getOrder() | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         return 2; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function setContainer(ContainerInterface $container = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function up(Schema $schema, QueryBag $queries) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $defaultStatuses = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             'new', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'qualified', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'canceled' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $connection = $this->container->get('doctrine')->getConnection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $oldStatuses = $connection->fetchAll('SELECT name, label FROM orocrm_sales_lead_status'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $newStatuses = $connection->fetchAll('SELECT id, priority FROM oro_enum_lead_status'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $oldStatuses = $this->buildOneDimensionArray($oldStatuses, 'name', 'label'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $newStatuses = $this->buildOneDimensionArray($newStatuses, 'id', 'priority'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $oldStatusesToAdd = array_diff_key($oldStatuses, $newStatuses); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if (count($oldStatusesToAdd) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $defaultStatuses = array_merge($defaultStatuses, array_keys($oldStatusesToAdd)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $priority = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (is_array($newStatuses) && count($newStatuses)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $priority = max($newStatuses) + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->updateLeadStatusTable($queries, $oldStatusesToAdd, $priority); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->updateLeadTable($queries, $defaultStatuses); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param QueryBag $queries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param array $statuses | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param int $priority | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     protected function updateLeadStatusTable($queries, $statuses, $priority) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         foreach ($statuses as $id => $name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $query  = 'INSERT INTO oro_enum_lead_status (id, name, priority, is_default) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                        VALUES (:id, :name, :priority, 0)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $params = ['id' => $id, 'name' => $name, 'priority' => $priority]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $types  = ['id' => Type::STRING, 'name' => Type::STRING, 'priority' => Type::INTEGER]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $migrationQuery = new ParametrizedSqlMigrationQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $migrationQuery->addSql( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $query, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 $params, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 $types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             $queries->addPostQuery($migrationQuery); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $priority++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param QueryBag $queries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param array $statuses | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     protected function updateLeadTable($queries, $statuses) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $query = 'UPDATE orocrm_sales_lead SET status_id = :status_id WHERE status_name = :status_name'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         foreach ($statuses as $status) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $migrationQuery = new ParametrizedSqlMigrationQuery(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $migrationQuery->addSql( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $query, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 ['status_id' => $status, 'status_name' => $status], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 ['status_id' => Type::STRING, 'status_name' => Type::STRING] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $queries->addPostQuery($migrationQuery); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param array $rows | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     protected function buildOneDimensionArray($rows, $key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         foreach ($rows as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $result[$row[$key]] = $row[$value]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.