Skip to content

Instantly share code, notes, and snippets.

@shlaikov
Created September 7, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shlaikov/3f54973ed15bb5d30cc750411a278512 to your computer and use it in GitHub Desktop.
Save shlaikov/3f54973ed15bb5d30cc750411a278512 to your computer and use it in GitHub Desktop.
ShopWindows Article, Catalog
<?php
class Article
{
private $posCode;
private $article;
private $xml;
public function __construct($posCode, $article)
{
$this->posCode = $posCode;
$this->article = $article;
$this->xml = $this->getArticle();
}
public function model()
{
return array(
'_id' => $this->article,
'xml' => $this->xml
);
}
private function getArticle()
{
$request = new GetArticle($this->posCode, $this->article);
return $request->execute();
}
}
<?php
class Catalog
{
private $id;
private $xml;
public function __construct($posCode) {
$this->id = $posCode;
$this->xml = $this->getCatalog();
}
public function model()
{
return array(
'_id' => $this->id,
'xml' => $this->xml
);
}
private function getCatalog()
{
$request = new GetCatalog($this->id);
return $request->execute();
}
}
<?php
class GetArticle
{
protected $posCode;
protected $articleAbbrev;
public function execute()
{
return $this->doRequest();
}
public function __construct($posCode, $article)
{
$this->posCode = $posCode;
$this->articleAbbrev = $article;
}
private function setUrl()
{
$url = PCInterface::BASE_URL;
$url .= 'pos/posservlet?artCode=';
$url .= $this->articleAbbrev;
$url .= '&posCode='. $this->posCode;
$url .= '&op=getArticle';
return $url;
}
private function doRequest()
{
$ch= curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->setUrl());
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close ($ch);
return($data);
}
public function getArticlesFromCatalog($posCode)
{
$request = new GetCatalog($posCode);
$data = $request->execute();
$catalog = new SimpleXMLElement($data);
foreach ($catalog->category->articles->article as $article) {
var_dump($article['code']);
}
}
}
<?php
class GetCatalog
{
protected $posCode;
public function execute()
{
return $this->doRequest();
}
public function __construct($posCode)
{
$this->posCode = $posCode;
}
private function setUrl()
{
$url = PCInterface::BASE_URL;
$url .= 'pos/posservlet?posCode=';
$url .= $this->posCode;
$url .= '&op=getCatalog';
return $url;
}
private function doRequest()
{
$ch= curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->setUrl());
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close ($ch);
return($data);
}
}
<?php
class UpdateCollections
{
private $params;
private $posCode;
private $articles;
private $collections = array('Article','Catalog');
public function update(array $params)
{
$this->params = $params;
$this->posCode = $params['POS_CODE'];
$this->articles = Project::getInstance()->getArticles($this->posCode);
$mongo = MongoConnector::connect();
$db = $mongo->selectDB($params['connectUrl']);
foreach ($this->articles as $article) {
foreach ($this->setCollections() as $collection) {
$db->createCollection($collection);
$cursor = $db->selectCollection($collection);
$cursor->save($this->fill($collection, $article));
}
}
$mongo->close();
}
private function fill($collection, $article)
{
switch ($collection) {
case 'Article':
$data = $this->getArticleData($this->posCode, $article);
return $data;
break;
case 'Catalog':
$data = $this->getCatalogData($this->posCode);
return $data;
break;
}
return null;
}
public function getArticleData($posCode, $id)
{
return (new Article($posCode, $id))->model();
}
public function getCatalogData($posCode)
{
return (new Catalog($posCode))->model();
}
private function setCollections()
{
return $this->collections;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment