PageRenderTime 57ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/php/Terminus/Models/Site.php

https://gitlab.com/blueprintmrk/cli
PHP | 555 lines | 337 code | 40 blank | 178 comment | 18 complexity | 5647ebbdbadf11b1eab57f0dba94c929 MD5 | raw file
  1. <?php
  2. namespace Terminus\Models;
  3. use Terminus;
  4. use TerminusCommand;
  5. use Terminus\Exceptions\TerminusException;
  6. use Terminus\Models\Organization;
  7. use Terminus\Models\TerminusModel;
  8. use Terminus\Models\Collections\Environments;
  9. use Terminus\Models\Collections\OrganizationSiteMemberships;
  10. use Terminus\Models\Collections\SiteOrganizationMemberships;
  11. use Terminus\Models\Collections\SiteUserMemberships;
  12. use Terminus\Models\Collections\Workflows;
  13. class Site extends TerminusModel {
  14. public $bindings;
  15. protected $environments;
  16. protected $org_memberships;
  17. protected $user_memberships;
  18. protected $workflows;
  19. private $features;
  20. private $tags;
  21. /**
  22. * Object constructor
  23. *
  24. * @param [stdClass] $attributes Attributes of this model
  25. * @param [array] $options Options to set as $this->key
  26. * @return [Site] $this
  27. */
  28. public function __construct($attributes = null, $options = array()) {
  29. if ($attributes == null) {
  30. $attributes = new \stdClass();
  31. }
  32. $must_haves = array(
  33. 'name',
  34. 'id',
  35. 'service_level',
  36. 'framework',
  37. 'created',
  38. 'memberships'
  39. );
  40. foreach ($must_haves as $must_have) {
  41. if (!isset($attributes->$must_have)) {
  42. $attributes->$must_have = null;
  43. }
  44. }
  45. parent::__construct($attributes, $options);
  46. $this->environments = new Environments(array('site' => $this));
  47. $this->org_memberships = new SiteOrganizationMemberships(
  48. array('site' => $this)
  49. );
  50. $this->user_memberships = new SiteUserMemberships(array('site' => $this));
  51. $this->workflows = new Workflows(array('owner' => $this));
  52. }
  53. /**
  54. * Adds payment instrument of given site
  55. *
  56. * @param [string] $uuid UUID of new payment instrument
  57. * @return [Workflow] $workflow Workflow object for the request
  58. */
  59. public function addInstrument($uuid) {
  60. $args = array(
  61. 'site' => $this->get('id'),
  62. 'params' => array(
  63. 'instrument_id' => $uuid
  64. )
  65. );
  66. $workflow = $this->workflows->create('associate_site_instrument', $args);
  67. return $workflow;
  68. }
  69. /**
  70. * Adds a tag to the site
  71. *
  72. * @param [string] $tag Tag to apply
  73. * @param [string] $org Organization to add the tag associateion to
  74. * @return [array] $response
  75. */
  76. public function addTag($tag, $org) {
  77. $params = array($tag => array('sites' => array($this->get('id'))));
  78. $response = TerminusCommand::simpleRequest(
  79. sprintf('organizations/%s/tags', $org),
  80. array('method' => 'put', 'data' => $params)
  81. );
  82. return $response;
  83. }
  84. /**
  85. * Apply upstream updates
  86. *
  87. * @param [string] $env_id Environment name
  88. * @param [boolean] $updatedb True to run update.php
  89. * @param [boolean] $xoption True to automatically resolve merge conflicts
  90. * @return [Workflow] $workflow
  91. */
  92. public function applyUpstreamUpdates(
  93. $env_id,
  94. $updatedb = true,
  95. $xoption = false
  96. ) {
  97. $params = array(
  98. 'updatedb' => $updatedb,
  99. 'xoption' => $xoption
  100. );
  101. $workflow = $this->workflows->create(
  102. 'apply_upstream_updates',
  103. array(
  104. 'environment' => $env_id,
  105. 'params' => $params
  106. )
  107. );
  108. return $workflow;
  109. }
  110. /**
  111. * Returns an array of attributes
  112. *
  113. * @return [stdClass] $atts['data']
  114. */
  115. public function attributes() {
  116. $path = 'attributes';
  117. $method = 'GET';
  118. $atts = TerminusCommand::request(
  119. 'sites',
  120. $this->get('id'),
  121. $path,
  122. $method
  123. );
  124. return $atts['data'];
  125. }
  126. /**
  127. * Fetch Binding info
  128. *
  129. * @param [string] $type Which sort of binding to retrieve
  130. * @return [array] $this->bindings
  131. */
  132. public function bindings($type = null) {
  133. if (empty($this->bindings)) {
  134. $response = TerminusCommand::simpleRequest(
  135. 'sites/' . $this->get('id') . '/' . $bindings
  136. );
  137. foreach ($response['data'] as $id => $binding) {
  138. $binding->id = $id;
  139. $this->bindings[$binding->type][] = $binding;
  140. }
  141. }
  142. if ($type) {
  143. if (isset($this->bindings[$type])) {
  144. return $this->bindings[$type];
  145. } else {
  146. return false;
  147. }
  148. }
  149. return $this->bindings;
  150. }
  151. /**
  152. * Create a new branch
  153. *
  154. * @param [string] $branch Name of new branch
  155. * @return [Workflow] $workflow
  156. */
  157. public function createBranch($branch) {
  158. $body = array('refspec' => sprintf('refs/heads/%s', $branch));
  159. $response = TerminusCommand::request(
  160. 'sites',
  161. $this->get('id'),
  162. 'code-branch',
  163. 'POST',
  164. compact('body')
  165. );
  166. return $response['data'];
  167. }
  168. /**
  169. * Deletes site
  170. *
  171. * @return [array] $response
  172. */
  173. public function delete() {
  174. $response = TerminusCommand::simpleRequest(
  175. 'sites/' . $this->get('id'),
  176. array('method' => 'delete')
  177. );
  178. return $response;
  179. }
  180. /**
  181. * Delete a branch from site remove
  182. *
  183. * @param [string] $branch Name of branch to remove
  184. * @return [void]
  185. */
  186. public function deleteBranch($branch) {
  187. $workflow = $this->workflows->create(
  188. 'delete_environment_branch',
  189. array(
  190. 'params' => array(
  191. 'environment_id' => $branch,
  192. )
  193. )
  194. );
  195. return $workflow;
  196. }
  197. /**
  198. * Delete a multidev environment
  199. *
  200. * @param [string] $env Name of environment to remove
  201. * @param [boolean] $delete_branch True to delete branch
  202. * @return [void]
  203. */
  204. public function deleteEnvironment($env, $delete_branch) {
  205. $workflow = $this->workflows->create(
  206. 'delete_cloud_development_environment',
  207. array(
  208. 'params' => array(
  209. 'environment_id' => $env,
  210. 'delete_branch' => $delete_branch,
  211. )
  212. )
  213. );
  214. return $workflow;
  215. }
  216. /**
  217. * Deletes site from cache
  218. *
  219. * @return [void]
  220. */
  221. public function deleteFromCache() {
  222. $this->collection->deleteSiteFromCache($this->get('name'));
  223. }
  224. /**
  225. * Fetches this object from Pantheon
  226. *
  227. * @param [boolean] $paged True to use paginated API requests
  228. * @return [Site] $this
  229. */
  230. public function fetch($paged = true) {
  231. $response = TerminusCommand::simpleRequest(
  232. sprintf('sites/%s?site_state=true', $this->get('id'))
  233. );
  234. $this->attributes = $response['data'];
  235. return $this;
  236. }
  237. /**
  238. * Returns given attribute, if present
  239. *
  240. * @param [string] $attribute Name of attribute requested
  241. * @return [mixed] $this->attributes->$attributes;
  242. */
  243. public function get($attribute) {
  244. if (isset($this->attributes->$attribute)) {
  245. return $this->attributes->$attribute;
  246. }
  247. return null;
  248. }
  249. /**
  250. * Returns a specific site feature value
  251. *
  252. * @param [string] $feature Feature to check
  253. * @return [mixed] $this->features[$feature]
  254. */
  255. public function getFeature($feature) {
  256. if (!isset($this->features)) {
  257. $response = TerminusCommand::simpleRequest(
  258. sprintf('sites/%s/features', $this->get('id'))
  259. );
  260. $this->features = (array)$response['data'];
  261. }
  262. if (isset($this->features[$feature])) {
  263. return $this->features[$feature];
  264. }
  265. return null;
  266. }
  267. /**
  268. * Returns all organization members of this site
  269. *
  270. * @return [array] Array of SiteOrganizationMemberships
  271. */
  272. public function getOrganizations() {
  273. $orgs = $this->org_memberships->all();
  274. return $orgs;
  275. }
  276. /**
  277. * Lists user memberships for this site
  278. *
  279. * @return [SiteUserMemberships] Collection of user memberships for this site
  280. */
  281. public function getSiteUserMemberships() {
  282. $this->user_memberships = $this->user_memberships->fetch();
  283. return $this->user_memberships;
  284. }
  285. /**
  286. * Returns tags from the site/org join
  287. *
  288. * @param [string] $org UUID of organization site belongs to
  289. * @return [array] $tags Tags in string format
  290. */
  291. public function getTags($org) {
  292. if (isset($this->tags)) {
  293. return $this->tags;
  294. }
  295. $org_site_member = new OrganizationSiteMemberships(
  296. array('organization' => new Organization(null, array('id' => $org)))
  297. );
  298. $org_site_member->fetch();
  299. $org = $org_site_member->get($this->get('id'));
  300. $tags = $org->get('tags');
  301. return $tags;
  302. }
  303. /**
  304. * Get upstream updates
  305. *
  306. * @return [stdClass] $response['data']
  307. */
  308. public function getUpstreamUpdates() {
  309. $response = TerminusCommand::simpleRequest(
  310. 'sites/' . $this->get('id') . '/code-upstream-updates'
  311. );
  312. return $response['data'];
  313. }
  314. /**
  315. * Imports a full-site archive
  316. *
  317. * @param [string] $url URL to import data from
  318. * @return [Workflow] $workflow
  319. */
  320. public function import($url) {
  321. $params = array(
  322. 'url' => $url,
  323. 'code' => 1,
  324. 'database' => 1,
  325. 'files' => 1,
  326. 'updatedb' => 1,
  327. );
  328. $workflow = $this->workflows->create(
  329. 'do_import',
  330. array(
  331. 'environment' => 'dev',
  332. 'params' => $params,
  333. )
  334. );
  335. return $workflow;
  336. }
  337. /**
  338. * Imports a database archive
  339. *
  340. * @param [string] $url URL to import data from
  341. * @return [Workflow] $workflow
  342. */
  343. public function importDatabase($url) {
  344. $workflow = $this->workflows->create(
  345. 'import_database',
  346. array(
  347. 'environment' => 'dev',
  348. 'params' => array('url' => $url),
  349. )
  350. );
  351. return $workflow;
  352. }
  353. /**
  354. * Imports a file archive
  355. *
  356. * @param [string] $url URL to import data from
  357. * @return [Workflow] $workflow
  358. */
  359. public function importFiles($url) {
  360. $workflow = $this->workflows->create(
  361. 'import_files',
  362. array(
  363. 'environment' => 'dev',
  364. 'params' => array('url' => $url),
  365. )
  366. );
  367. return $workflow;
  368. }
  369. /**
  370. * Load site info
  371. *
  372. * @param [string] $key Set to retrieve a specific attribute as named
  373. * @return [array] $info
  374. */
  375. public function info($key = null) {
  376. $info = array(
  377. 'id' => $this->get('id'),
  378. 'name' => null,
  379. 'label' => null,
  380. 'created' => null,
  381. 'framework' => null,
  382. 'organization' => null,
  383. 'service_level' => null,
  384. 'upstream' => null,
  385. 'php_version' => null,
  386. 'holder_type' => null,
  387. 'holder_id' => null,
  388. 'owner' => null,
  389. );
  390. foreach ($info as $info_key => $datum) {
  391. if ($datum == null) {
  392. $info[$info_key] = $this->get($info_key);
  393. }
  394. }
  395. if ($key) {
  396. return isset($info[$key]) ? $info[$key] : null;
  397. } else {
  398. return $info;
  399. }
  400. }
  401. /**
  402. * Retrieve New Relic Info
  403. *
  404. * @return [stdClass] $response['data']
  405. */
  406. public function newRelic() {
  407. $path = 'new-relic';
  408. $response = TerminusCommand::simpleRequest(
  409. 'sites/' . $this->get('id') . '/new-relic'
  410. );
  411. return $response['data'];
  412. }
  413. /**
  414. * Returns all organization members of this site
  415. *
  416. * @param [string] $uuid UUID of organization to check for
  417. * @return [boolean] True if organization is a member of this site
  418. */
  419. public function organizationIsMember($uuid) {
  420. $org_ids = $this->org_memberships->ids();
  421. $org_is_member = in_array($uuid, $org_ids);
  422. return $org_is_member;
  423. }
  424. /**
  425. * Removes payment instrument of given site
  426. *
  427. * @params [string] $uuid UUID of new payment instrument
  428. * @return [Workflow] $workflow Workflow object for the request
  429. */
  430. public function removeInstrument() {
  431. $args = array('site' => $this->get('id'),);
  432. $workflow = $this->workflows->create('disassociate_site_instrument', $args);
  433. return $workflow;
  434. }
  435. /**
  436. * Removes a tag to the site
  437. *
  438. * @param [string] $tag Tag to remove
  439. * @param [string] $org Organization to remove the tag associateion from
  440. * @return [array] $response
  441. */
  442. public function removeTag($tag, $org) {
  443. $response = TerminusCommand::simpleRequest(
  444. sprintf(
  445. 'organizations/%s/tags/%s/sites?entity=%s',
  446. $org,
  447. $tag,
  448. $this->get('id')
  449. ),
  450. array('method' => 'delete')
  451. );
  452. return $response;
  453. }
  454. /**
  455. * Owner handler
  456. *
  457. * @param [string] $owner UUID of new owner of site
  458. * @return [stdClass] $data['data']
  459. */
  460. public function setOwner($owner = null) {
  461. $new_owner = $this->user_memberships->get($owner);
  462. if ($new_owner == null) {
  463. $message = 'The owner must be a team member. Add them with `site team`';
  464. throw new TerminusException($message);
  465. }
  466. $workflow = $this->workflows->create(
  467. 'promote_site_user_to_owner',
  468. array(
  469. 'params' => array(
  470. 'user_id' => $new_owner->get('id')
  471. )
  472. )
  473. );
  474. return $workflow;
  475. }
  476. /**
  477. * Just the code branches
  478. *
  479. * @return [stdClass] $data['data']
  480. */
  481. public function tips() {
  482. $path = 'code-tips';
  483. $data = TerminusCommand::request('sites', $this->get('id'), $path, 'GET');
  484. return $data['data'];
  485. }
  486. /**
  487. * Update service level
  488. *
  489. * @param [string] $level Level to set service on site to
  490. * @return [stdClass] $response['data']
  491. */
  492. public function updateServiceLevel($level) {
  493. $path = 'service-level';
  494. $method = 'PUT';
  495. $body = $level;
  496. $response = TerminusCommand::request(
  497. 'sites',
  498. $this->get('id'),
  499. $path,
  500. $method,
  501. compact('body')
  502. );
  503. return $response['data'];
  504. }
  505. /**
  506. * Verifies if the given framework is in use
  507. *
  508. * @param [string] $framework_name Name of framework to verify
  509. * @return [boolean] $has_framework
  510. */
  511. private function hasFramework($framework_name) {
  512. $has_framework = ($framework_name == $this->get('framework'));
  513. return $has_framework;
  514. }
  515. }