Completed
Push — master ( 3b7290...73d268 )
by
unknown
11:56
created

AddOpportunityStatus::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\Migrations\Schema\v1_22;
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\EntityExtendBundle\Migration\Extension\ExtendExtension;
13
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtensionAwareInterface;
14
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
15
use Oro\Bundle\MigrationBundle\Migration\Migration;
16
use Oro\Bundle\MigrationBundle\Migration\ParametrizedSqlMigrationQuery;
17
use Oro\Bundle\MigrationBundle\Migration\QueryBag;
18
use Oro\Bundle\EntityBundle\EntityConfig\DatagridScope;
19
use Oro\Bundle\EntityExtendBundle\Migration\OroOptions;
20
use Oro\Bundle\EntityExtendBundle\Migration\ExtendOptionsManager;
21
22
use OroCRM\Bundle\SalesBundle\Entity\Opportunity;
23
24
class AddOpportunityStatus implements
25
    Migration,
26
    ExtendExtensionAwareInterface,
27
    ContainerAwareInterface,
28
    OrderedMigrationInterface
29
{
30
    /** @var ContainerInterface */
31
    protected $container;
32
33
    protected $extendExtension;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getOrder()
39
    {
40
        return 1;
41
    }
42
43
    /**
44
     * @param ExtendExtension $extendExtension
45
     */
46
    public function setExtendExtension(ExtendExtension $extendExtension)
47
    {
48
        $this->extendExtension = $extendExtension;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function setContainer(ContainerInterface $container = null)
55
    {
56
        $this->container = $container;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function up(Schema $schema, QueryBag $queries)
63
    {
64
        /** @var ExtendOptionsManager $extendOptionsManager */
65
        $extendOptionsManager = $this->container->get('oro_entity_extend.migration.options_manager');
66
        $extendOptionsManager->removeColumnOptions('orocrm_sales_opportunity', 'status');
67
68
        self::addStatusField($schema, $this->extendExtension, $queries);
69
70
        $statusMapping = [
71
            'in_progress' => 'in_progress',
72
            'won' => 'won',
73
            'lost' => 'lost'
74
        ];
75
        $query = 'UPDATE orocrm_sales_opportunity SET status_id = :status_id WHERE status_name = :status_name';
76
        foreach ($statusMapping as $oldStatus => $newStatus) {
77
            $migrationQuery = new ParametrizedSqlMigrationQuery();
78
            $migrationQuery->addSql(
79
                $query,
80
                ['status_id' => $newStatus, 'status_name' => $oldStatus],
81
                ['status_id' => Type::STRING, 'status_name' => Type::STRING]
82
            );
83
            $queries->addPostQuery($migrationQuery);
84
        }
85
    }
86
87
    /**
88
     * @param Schema $schema
89
     * @param ExtendExtension $extendExtension
90
     * @param QueryBag $queries
91
     */
92
    public static function addStatusField(Schema $schema, ExtendExtension $extendExtension, QueryBag $queries)
93
    {
94
        $enumTable = $extendExtension->addEnumField(
95
            $schema,
96
            'orocrm_sales_opportunity',
97
            'status',
98
            Opportunity::INTERNAL_STATUS_CODE,
99
            false,
100
            false,
101
            [
102
                'extend' => ['owner' => ExtendScope::OWNER_SYSTEM],
103
                'datagrid' => ['is_visible' => DatagridScope::IS_VISIBLE_TRUE],
104
                'dataaudit' => ['auditable' => true],
105
                'importexport' => ["order" => 90, "short" => true]
106
            ]
107
        );
108
109
        $options = new OroOptions();
110
        $options->set(
111
            'enum',
112
            'immutable_codes',
113
            [
114
                'in_progress',
115
                'won',
116
                'lost'
117
            ]
118
        );
119
120
        $enumTable->addOption(OroOptions::KEY, $options);
0 ignored issues
show
Documentation introduced by
$options is of type object<Oro\Bundle\Entity...e\Migration\OroOptions>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
        $statuses = [
122
            'identification_alignment' => 'Identification & Alignment',
123
            'needs_analysis' => 'Needs Analysis',
124
            'solution_development' => 'Solution Development',
125
            'negotiation' => 'Negotiation',
126
            'in_progress' => 'In Progress',
127
            'won' => 'Closed Won',
128
            'lost' => 'Closed Lost'
129
        ];
130
        $defaultValue = 'in_progress';
131
        $query = 'INSERT INTO oro_enum_opportunity_status (id, name, priority, is_default)
132
                  VALUES (:id, :name, :priority, :is_default)';
133
        $i = 1;
134
        foreach ($statuses as $key => $value) {
135
            $dropFieldsQuery = new ParametrizedSqlMigrationQuery();
136
            $dropFieldsQuery->addSql(
137
                $query,
138
                ['id' => $key, 'name' => $value, 'priority' => $i, 'is_default' => $defaultValue === $key],
139
                [
140
                    'id' => Type::STRING,
141
                    'name' => Type::STRING,
142
                    'priority' => Type::INTEGER,
143
                    'is_default' => Type::BOOLEAN
144
                ]
145
            );
146
            $queries->addQuery($dropFieldsQuery);
147
            $i++;
148
        }
149
    }
150
}
151