Skip to content

Commit 420bf0b

Browse files
author
Rafael Grigorian
committed
Fixed #24 & Fixed #25
1 parent 3b3c0c8 commit 420bf0b

File tree

7 files changed

+407
-0
lines changed

7 files changed

+407
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Console\Command;
4+
5+
use Magento\Store\Model\StoreManagerInterface;
6+
use Symfony\Component\Console\Command\Command;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
abstract class AbstractCommand extends Command {
11+
12+
protected $_storeManager;
13+
14+
public function __construct (
15+
StoreManagerInterface $storeManager
16+
) {
17+
// Call the parent constructor
18+
parent::__construct ();
19+
// Save injected classes internally
20+
$this->_storeManager = $storeManager;
21+
}
22+
23+
/**
24+
* This method looks though all the stores that are setup in Magento. It
25+
* then extracts the domain name from the store's base URL.
26+
* @return array All domains for all stores
27+
*/
28+
protected function _getDomainNames () {
29+
$domains = array ();
30+
$stores = $this->_storeManager->getStores ();
31+
foreach ( $stores as $store ) {
32+
$domain = parse_url ( $store->getBaseUrl () ) ["host"];
33+
array_push ( $domains, $domain );
34+
}
35+
$domains = array_unique ( $domains );
36+
sort ( $domains );
37+
return $domains;
38+
}
39+
40+
/**
41+
* This method looks though all the stores that are setup in Magento. It
42+
* then returns the store object that matches the domain name.
43+
* @param string domain Domain name
44+
* @return array|false All domains for all stores
45+
*/
46+
protected function _getStoreByDomain ( $domain ) {
47+
$stores = $this->_storeManager->getStores ();
48+
foreach ( $stores as $store ) {
49+
$host = parse_url ( $store->getBaseUrl () ) ["host"];
50+
if ( $host === $domain ) {
51+
return $store;
52+
}
53+
}
54+
return false;
55+
}
56+
57+
/**
58+
* This method is used to handle error messages and failed requests.
59+
* @param Object response Response from cloudflare
60+
* @param InputInterface input The input interface
61+
* @param OutputInterface output The output interface
62+
* @return bool Was request successful?
63+
*/
64+
protected function _isSuccessful ( $response, $input, $output ) {
65+
if ( property_exists ( $response, "success" ) ) {
66+
if ( property_exists ( $response, "errors" ) ) {
67+
foreach ( $response->errors as $error ) {
68+
$output->writeln ("Error [$error->code]: $error->message");
69+
}
70+
}
71+
return $response->success;
72+
}
73+
else if ( property_exists ( $response, "error" ) ) {
74+
$output->writeln ("Error [$response->code]: $response->error");
75+
}
76+
return false;
77+
}
78+
79+
/**
80+
* This method is here because it interfaces with the abstract parent class. It takes in an
81+
* input and output interface and it runs the command.
82+
* @param InputInterface input The input interface
83+
* @param OutputInterface output The output interface
84+
* @return void
85+
*/
86+
public function execute ( InputInterface $input, OutputInterface $output ) {
87+
$domain = $input->getOption ("domain");
88+
if ( !$domain ) {
89+
$output->writeln ("Error: please pass domain name with --domain option.");
90+
return $this;
91+
}
92+
$domains = $this->_getDomainNames ();
93+
if ( !in_array ( $domain, $domains ) ) {
94+
$output->writeln ("Error: invalid domain name, available options are:");
95+
$output->writeln ("");
96+
foreach ( $domains as $d) {
97+
$output->writeln ("- $d");
98+
}
99+
$output->writeln ("");
100+
return $this;
101+
}
102+
$this->runCommand ( $input, $output );
103+
}
104+
105+
protected abstract function runCommand (
106+
InputInterface $input,
107+
OutputInterface $output
108+
);
109+
110+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Console\Command\Caching\DevelopmentMode;
4+
5+
use JetRails\Cloudflare\Console\Command\AbstractCommand;
6+
use JetRails\Cloudflare\Model\Adminhtml\Api\Caching\DevelopmentMode;
7+
use Magento\Store\Model\StoreManagerInterface;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
class Get extends AbstractCommand {
13+
14+
protected $_storeManager;
15+
protected $_model;
16+
17+
public function __construct (
18+
StoreManagerInterface $storeManager,
19+
DevelopmentMode $model
20+
) {
21+
// Call the parent constructor
22+
parent::__construct ( $storeManager );
23+
// Save injected classes internally
24+
$this->_storeManager = $storeManager;
25+
$this->_model = $model;
26+
}
27+
28+
/**
29+
* Inside we set the command name and set the command description.
30+
* @return void
31+
*/
32+
protected function configure () {
33+
// Register the command and set the arguments
34+
$options = [
35+
new InputOption (
36+
"domain",
37+
null,
38+
InputOption::VALUE_REQUIRED,
39+
"What is the domain name?"
40+
)
41+
];
42+
$this
43+
->setName ("cloudflare:caching:development-mode:get")
44+
->setDescription ("Is site currently in development mode?")
45+
->setDefinition ( $options );
46+
parent::configure ();
47+
}
48+
49+
/**
50+
* This method is here because it interfaces with the abstract parent class. It takes in an
51+
* input and output interface and it runs the command.
52+
* @param InputInterface input The input interface
53+
* @param OutputInterface output The output interface
54+
* @return void
55+
*/
56+
public function runCommand ( InputInterface $input, OutputInterface $output ) {
57+
$old = $this->_storeManager->getStore ()->getId ();
58+
$domain = $input->getOption ("domain");
59+
$store = $this->_getStoreByDomain ( $domain );
60+
$this->_storeManager->setCurrentStore ( $store->getId () );
61+
$response = $this->_model->getValue ();
62+
$this->_storeManager->setCurrentStore ( $old );
63+
if ( $this->_isSuccessful ( $response, $input, $output ) ) {
64+
$value = "<options=underscore>" . $response->result->value . "</>";
65+
$output->writeln ( "Development-Mode is set to: $value" );
66+
}
67+
}
68+
69+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Console\Command\Caching\DevelopmentMode;
4+
5+
use JetRails\Cloudflare\Console\Command\AbstractCommand;
6+
use JetRails\Cloudflare\Model\Adminhtml\Api\Caching\DevelopmentMode;
7+
use Magento\Store\Model\StoreManagerInterface;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
class Set extends AbstractCommand {
13+
14+
protected $_storeManager;
15+
protected $_model;
16+
17+
public function __construct (
18+
StoreManagerInterface $storeManager,
19+
DevelopmentMode $model
20+
) {
21+
// Call the parent constructor
22+
parent::__construct ( $storeManager );
23+
// Save injected classes internally
24+
$this->_storeManager = $storeManager;
25+
$this->_model = $model;
26+
}
27+
28+
/**
29+
* Inside we set the command name and set the command description.
30+
* @return void
31+
*/
32+
protected function configure () {
33+
// Register the command and set the arguments
34+
$options = [
35+
new InputOption (
36+
"domain",
37+
null,
38+
InputOption::VALUE_REQUIRED,
39+
"What is the domain name?"
40+
),
41+
new InputOption (
42+
"state",
43+
null,
44+
InputOption::VALUE_REQUIRED,
45+
"Set development-mode 'on' or 'off'?"
46+
)
47+
];
48+
$this
49+
->setName ("cloudflare:caching:development-mode:set")
50+
->setDescription ("Set development mode to 'on' or 'off'")
51+
->setDefinition ( $options );
52+
parent::configure ();
53+
}
54+
55+
/**
56+
* This method is here because it interfaces with the abstract parent class. It takes in an
57+
* input and output interface and it runs the command.
58+
* @param InputInterface input The input interface
59+
* @param OutputInterface output The output interface
60+
* @return void
61+
*/
62+
public function runCommand ( InputInterface $input, OutputInterface $output ) {
63+
$domain = $input->getOption ("domain");
64+
$state = $input->getOption ("state");
65+
if ( !in_array ( $state, [ "on", "off" ] ) ) {
66+
$output->writeln ("Error: specify 'on' or 'off' for 'state' value");
67+
return $this;
68+
}
69+
$old = $this->_storeManager->getStore ()->getId ();
70+
$store = $this->_getStoreByDomain ( $domain );
71+
$this->_storeManager->setCurrentStore ( $store->getId () );
72+
$response = $this->_model->setValue ( $state === "on" );
73+
$this->_storeManager->setCurrentStore ( $old );
74+
if ( $this->_isSuccessful ( $response, $input, $output ) ) {
75+
$value = "<options=underscore>" . $response->result->value . "</>";
76+
$output->writeln ( "Development-Mode is set to: $value" );
77+
}
78+
}
79+
80+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace JetRails\Cloudflare\Console\Command\Caching\PurgeCache;
4+
5+
use JetRails\Cloudflare\Console\Command\AbstractCommand;
6+
use JetRails\Cloudflare\Model\Adminhtml\Api\Caching\PurgeCache;
7+
use Magento\Store\Model\StoreManagerInterface;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Input\InputOption;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
class Everything extends AbstractCommand {
13+
14+
protected $_storeManager;
15+
protected $_model;
16+
17+
public function __construct (
18+
StoreManagerInterface $storeManager,
19+
PurgeCache $model
20+
) {
21+
// Call the parent constructor
22+
parent::__construct ( $storeManager );
23+
// Save injected classes internally
24+
$this->_storeManager = $storeManager;
25+
$this->_model = $model;
26+
}
27+
28+
/**
29+
* Inside we set the command name and set the command description.
30+
* @return void
31+
*/
32+
protected function configure () {
33+
// Register the command and set the arguments
34+
$options = [
35+
new InputOption (
36+
"domain",
37+
null,
38+
InputOption::VALUE_REQUIRED,
39+
"What is the domain name?"
40+
)
41+
];
42+
$this
43+
->setName ("cloudflare:caching:purge-cache:everything")
44+
->setDescription ("Purge all Cloudflare cache")
45+
->setDefinition ( $options );
46+
parent::configure ();
47+
}
48+
49+
/**
50+
* This method is here because it interfaces with the abstract parent class. It takes in an
51+
* input and output interface and it runs the command.
52+
* @param InputInterface input The input interface
53+
* @param OutputInterface output The output interface
54+
* @return void
55+
*/
56+
public function runCommand ( InputInterface $input, OutputInterface $output ) {
57+
$old = $this->_storeManager->getStore ()->getId ();
58+
$domain = $input->getOption ("domain");
59+
$store = $this->_getStoreByDomain ( $domain );
60+
$this->_storeManager->setCurrentStore ( $store->getId () );
61+
$response = $this->_model->purgeEverything ();
62+
$this->_storeManager->setCurrentStore ( $old );
63+
if ( $this->_isSuccessful ( $response, $input, $output ) ) {
64+
$output->writeln ("Successfully purged everything!");
65+
}
66+
}
67+
68+
}

0 commit comments

Comments
 (0)