Odyssey
2019101800_updDMITrustedMaster.php
1 <?php
2 /**
3  * Notes:
4  * 1. Anything complicated needs to be done with SQL.
5  * 2. Always have a check to know if the migration needs to occur (up or down).
6  * 3. Use up() and down(), not change(), because of the SQL
7  */
8 use Phinx\Migration\AbstractMigration;
9 
10 /**
11  * Updates trusted master to include testers for DMI
12  */
13 class updDMITrustedMaster extends AbstractMigration {
14 
15  /** @var string */
16  protected $trusted_table = "cutrustedmaster";
17  /** @var array - use in part for updates, completely for insert */
18  protected $trusted_default = [
19  'trustedid' => 'DMI',
20  'trustedvendor' => 'Dovenmuehle',
21  'trustedtype' => 'O',
22  'hcuinterface' => '',
23  'trustedfields' => [
24  'prodGUID' => [
25  'Type' => 'string',
26  'Default' => '',
27  'Message' => 'Client ID from DMI - production'
28  ],
29  'testGUID' => [
30  'Type' => 'string',
31  'Default' => '',
32  'Message' => 'Client ID from DMI - testing'
33  ],
34  'pilot' => [
35  'Type' => 'digits',
36  'Default' => '0',
37  'Message' => '1=testing 0=production'
38  ],
39  'prodSSOURL' => [
40  'Type' => 'string',
41  'Default' => 'https://mtgsvc.com/SSO/SSOLogin.aspx',
42  'Message' => 'SSO URL - production'
43  ],
44  'testSSOURL' => [
45  'Type' => 'string',
46  'Default' => 'https://mtgsvc.tv/SSO/SSOLogin.aspx',
47  'Message' => 'SSO URL - testing'
48  ],
49  'prodAuthURL' => [
50  'Type' => 'string',
51  'Default' => 'https://mtgsvc.com/SSO/SSOCertify.aspx',
52  'Message' => 'authentication URL - production'
53  ],
54  'testAuthURL' => [
55  'Type' => 'string',
56  'Default' => 'https://mtgsvc.tv/SSO/SSOCertify.aspx',
57  'Message' => 'authentication URL - testing'
58  ],
59  'hcuLogging' => [
60  'Type' => 'string',
61  'Default' => ''
62  ],
63  'hcuCert' => [
64  'Type' => 'string',
65  'Default' => 'prod/certs/homecu/HomeCUCF.crt',
66  'Message' => 'HomeCU server certificate is passed as client cert.'
67  ],
68  'hcuKey' => [
69  'Type' => 'string',
70  'Default' => 'prod/certs/homecu/HomeCUCF.key',
71  'Message' => 'HomeCU server certificate key to pass with server cert as client cert'
72  ],
73  'testers' => [
74  'Type' => 'string',
75  'Default' => '',
76  'Message' => 'csv list of users to force test mode'
77  ]
78  ]
79  ];
80 
81  /**
82  * See if Dovenmuehle DMI exists, if so, update it to force the testers entry and
83  * updated default values for URLS
84  *
85  * Since trustedid is unique key to table, counting on only one DMI entry
86  * @return void
87  */
88  public function up() {
89 
90  if ($this->hasTable($this->trusted_table)) {
91 
92  $def = $this->trusted_default;
93  $sql = "select * from {$this->trusted_table} where trustedid = '{$def['trustedid']}'";
94 
95  # note appears that fetchRow always returns count of 1, even if no row is found.
96  # using fetchAll even though I expect max 1 record (trustedid is unique key)
97 
98  $trusted_row = $this->fetchAll($sql);
99 
100  if (count($trusted_row) > 0) {
101  $sql = "update {$this->trusted_table} set trustedfields = E'"
102  . json_encode($def['trustedfields'])
103  . "' where trustedid = '{$def['trustedid']}'";
104 
105  echo " updating {$def['trustedid']} trusted master\n";
106  $this->query($sql);
107 
108  } else {
109 
110  $sql = "insert into {$this->trusted_table} (trustedid, trustedvendor, trustedtype, hcuinterface, trustedfields)"
111  . " values('{$def['trustedid']}', '{$def['trustedvendor']}', '{$def['trustedtype']}',"
112  . " E'{$def['hcuinterface']}', E'" . json_encode($def['trustedfields']) . "')";
113 
114  echo " inserting {$def['trustedid']} trusted master\n";
115  $this->query($sql);
116  }
117  }
118  }
119 
120  /**
121  * **Assuming** the previous state was properly added without testers, re-add it as such.
122  * @return void
123  */
124  public function down() {
125 
126  $def = $this->trusted_default;
127  unset ($def['trustedfields']['testers']);
128 
129  $sql = "update {$this->trusted_table} set trustedfields = E'"
130  . json_encode($def['trustedfields'])
131  . "' where trustedid = '{$def['trustedid']}'";
132 
133  echo " reversing update {$def['trustedid']} trusted master\n";
134  $this->query($sql);
135  }
136 }
137 
138 ?>