Skip to content

Commit

Permalink
feat: basic logic
Browse files Browse the repository at this point in the history
  • Loading branch information
altynbek07 committed May 3, 2020
1 parent 9d0790c commit 0fc0d29
Show file tree
Hide file tree
Showing 33 changed files with 1,583 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.2.5"
"php": "^7.2.5",
"illuminate/support": "^6.0|^7.0",
"carono/commerceml": "^0.2.5"
},
"autoload": {
"psr-4": {
"Altynbek07\\Exchange1C\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Altynbek07\\Exchange1C\\Exchange1CServiceProvider"
]
}
}
}
}
36 changes: 36 additions & 0 deletions config/exchange-1c.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

return [
/**
*
*/
'exchange_path' => 'exchange-1c',
/**
*
*/
'import_dir' => storage_path('app/exchange-1c'),
/**
*
*/
'login' => env('EXCHANGE_1C_LOGIN', 'admin'),
/**
*
*/
'password' => env('EXCHANGE_1C_PASSWORD', 'admin'),
/**
*
*/
'use_zip' => false,
/**
*
*/
'file_part' => 0,
/**
*
*/
'models' => [
\Altynbek07\Exchange1C\Interfaces\GroupInterface::class => \App\Models\Category::class,
\Altynbek07\Exchange1C\Interfaces\ProductInterface::class => \App\Models\Product::class,
\Altynbek07\Exchange1C\Interfaces\OfferInterface::class => \App\Models\Offer::class,
],
];
6 changes: 6 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$path = config('exchange-1c.exchange_path', 'exchange-1c');

Route::group(['middleware' => [\Illuminate\Session\Middleware\StartSession::class]], function () use ($path) {
Route::match(['get', 'post'], $path, Altynbek07\Exchange1C\Controllers\ImportController::class.'@request');
});
156 changes: 156 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace Altynbek07\Exchange1C;

/**
* Class Config.
*/
class Config
{
/**
* @var string
*/
private $importDir = 'import_dir';

/**
* @var string
*/
private $login = 'admin';

/**
* @var string
*/
private $password = 'admin';

/**
* @var bool
*/
private $useZip = false;

/**
* @var int
*/
private $filePart = 0;

/**
* @var array
*/
private $models = [
\Altynbek07\Exchange1C\Interfaces\GroupInterface::class => null,
\Altynbek07\Exchange1C\Interfaces\ProductInterface::class => null,
\Altynbek07\Exchange1C\Interfaces\OfferInterface::class => null,
];

/**
* Config constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
$this->configure($config);
}

/**
* Overrides default configuration settings.
*
* @param array $config
*/
private function configure(array $config = []): void
{
foreach ($config as $param => $value) {
$property = $this->toCamelCase($param);
if (property_exists(self::class, $property)) {
$this->$property = $value;
}
}
}

/**
* @return string
*/
public function getImportDir(): string
{
return $this->importDir;
}

/**
* @return string
*/
public function getLogin(): string
{
return $this->login;
}

/**
* @return string
*/
public function getPassword(): string
{
return $this->password;
}

/**
* @return bool
*/
public function isUseZip(): bool
{
return $this->useZip;
}

/**
* @return int
*/
public function getFilePart(): int
{
return $this->filePart;
}

/**
* @return array
*/
public function getModels(): array
{
return $this->models;
}

/**
* @param string $modelName
*
* @return null|string
*/
public function getModelClass(string $modelName): ?string
{
if (isset($this->models[$modelName])) {
return $this->models[$modelName];
}

return null;
}

/**
* @param string $filename
*
* @return string
*/
public function getFullPath(string $filename): string
{
return $this->getImportDir() . DIRECTORY_SEPARATOR . $filename;
}

/**
* Translates a string with underscores into camel case (e.g. first_name -&gt; firstName).
*
* @param string $str String in underscore format
*
* @return string $str translated into camel caps
*/
private function toCamelCase($str): string
{
$func = function ($c) {
return strtoupper($c[1]);
};

return preg_replace_callback('/_([a-z])/', $func, $str);
}
}
50 changes: 50 additions & 0 deletions src/Controllers/ImportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Altynbek07\Exchange1C\Controllers;

use Altynbek07\Exchange1C\Exceptions\Exchange1CException;
use Altynbek07\Exchange1C\Services\CatalogService;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

/**
* Class ImportController.
*/
class ImportController extends Controller
{
/**
* @param Request $request
* @param CatalogService $service
*
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
*/
public function request(Request $request, CatalogService $service)
{
$mode = $request->get('mode');
$type = $request->get('type');

try {
if ($type == 'catalog') {
if (!method_exists($service, $mode)) {
throw new Exchange1CException('not correct request, class ExchangeCML not found');
}

$response = $service->$mode();
\Log::debug('exchange_1c: $response='."\n".$response);

return response($response, 200, ['Content-Type', 'text/plain']);
} else {
throw new \LogicException(sprintf('Logic for method %s not released', $type));
}
} catch (Exchange1CException $e) {
\Log::error("exchange_1c: failure \n".$e->getMessage()."\n".$e->getFile()."\n".$e->getLine()."\n");

$response = "failure\n";
$response .= $e->getMessage()."\n";
$response .= $e->getFile()."\n";
$response .= $e->getLine()."\n";

return response($response, 500, ['Content-Type', 'text/plain']);
}
}
}
19 changes: 19 additions & 0 deletions src/Events/AbstractEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Altynbek07\Exchange1C\Events;

/**
* Class AbstractEventInterface.
*/
abstract class AbstractEventInterface implements \Altynbek07\Exchange1C\Interfaces\EventInterface
{
public const NAME = self::class;

/**
* @return string
*/
public function getName(): string
{
return self::NAME;
}
}
23 changes: 23 additions & 0 deletions src/Events/AfterOffersSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Altynbek07\Exchange1C\Events;

class AfterOffersSync extends AbstractEventInterface
{
const NAME = 'after.offers.sync';

/**
* @var array
*/
public $ids;

/**
* AfterOffersSync constructor.
*
* @param array $ids
*/
public function __construct(array $ids = [])
{
$this->ids = $ids;
}
}
23 changes: 23 additions & 0 deletions src/Events/AfterProductsSync.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Altynbek07\Exchange1C\Events;

class AfterProductsSync extends AbstractEventInterface
{
const NAME = 'after.products.sync';

/**
* @var array
*/
public $ids;

/**
* AfterProductsSync constructor.
*
* @param array $ids
*/
public function __construct(array $ids = [])
{
$this->ids = $ids;
}
}
33 changes: 33 additions & 0 deletions src/Events/AfterUpdateOffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Altynbek07\Exchange1C\Events;

use Altynbek07\Exchange1C\Interfaces\OfferInterface;
use Zenwalker\CommerceML\Model\Offer;

class AfterUpdateOffer extends AbstractEventInterface
{
const NAME = 'after.update.offer';

/**
* @var OfferInterface
*/
public $model;

/**
* @var Offer
*/
public $offer;

/**
* AfterUpdateOffer constructor.
*
* @param OfferInterface $model
* @param Offer $offer
*/
public function __construct(OfferInterface $model, Offer $offer)
{
$this->model = $model;
$this->offer = $offer;
}
}
Loading

0 comments on commit 0fc0d29

Please sign in to comment.