src/CarCutter/ConfigManager.php line 392

Open in your IDE?
  1. <?php
  2. /**
  3.  * Carcutter Data Platform
  4.  * Configuration Manager
  5.  */
  6. namespace App\CarCutter;
  7. use League\Flysystem\FilesystemAdapter;
  8. use League\Flysystem\Filesystem;
  9. use League\Flysystem\Ftp\FtpAdapter;
  10. use Pimcore\Model\DataObject\CustomerConfig;
  11. use League\Flysystem\PhpseclibV3\SftpAdapter;
  12. use League\Flysystem\PhpseclibV3\SftpConnectionProvider;
  13. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnAutoGestion;
  14. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnBee2Link;
  15. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnFTPSettings;
  16. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnKeplerVO;
  17. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnPlanetVO;
  18. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnHomeNet;
  19. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnMaxDigital;
  20. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnDAT;
  21. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnProMax;
  22. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnDealerSearch;
  23. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnDealersync;
  24. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnCarbase;
  25. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnWayke;
  26. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnFastback;
  27. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnAutoTelex;
  28. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnOpenFlex;
  29. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnWebhook;
  30. use Pimcore\Model\DataObject\Fieldcollection\Data\ConnDealerCenter;
  31. use Throwable;
  32. class ConfigManager
  33. {
  34.     public static function loadConfigByName(string $name) : ?CustomerConfig
  35.     {
  36.         $list = new CustomerConfig\Listing();
  37.         $list->setCondition('name = :name and enabled = 1', [
  38.             'name' => $name
  39.         ]);
  40.         if($results $list->load()) {
  41.             return $results[0];
  42.         }
  43.         return null;
  44.     }
  45.     public static function loadConfigByToken(string $token) : ?CustomerConfig
  46.     {
  47.         $list = new CustomerConfig\Listing();
  48.         $list->setCondition('token = :token and enabled = 1', [
  49.             'token' => $token
  50.         ]);
  51.         if($results $list->load()) {
  52.             return $results[0];
  53.         }
  54.         return null;
  55.     }
  56.     public static function loadAllConfigsByToken(string $token): array
  57.     {
  58.         $list = new CustomerConfig\Listing();
  59.         $list->setCondition('token = :token AND enabled = 1', [
  60.             'token' => $token
  61.         ]);
  62.         return $list->load();
  63.     }
  64.     public static function loadValidConfigs() : array
  65.     {
  66.         $list = new CustomerConfig\Listing();
  67.         $list->setCondition('enabled = 1');
  68.         return $list->load();
  69.     }
  70.     public static function getUploadAdapter(CustomerConfig $configstring $type ''bool $raw false) : mixed
  71.     {
  72.         return self::getAdapter($config, ['out''bi'], $type$raw);
  73.     }
  74.     
  75.     public static function getProMaxAdapter(CustomerConfig $config) : mixed
  76.     {
  77.         $availableConnections self::getConnections($configConnProMax::class, []);
  78.         if(count($availableConnections) > 0) {
  79.             return $availableConnections;
  80.         }
  81.         return null;
  82.     }
  83.     public static function getDealerCarSearchAdapter(CustomerConfig $config) : mixed
  84.     {
  85.         $availableConnections self::getConnections($configConnDealerSearch::class, []);
  86.         echo count($availableConnections)." DealerCarSearch Files found\n";
  87.         if(count($availableConnections) > 0) {
  88.             return $availableConnections;
  89.         }
  90.         return null;
  91.     }
  92.     public static function getDealerSyncAdapter(CustomerConfig $config) : mixed
  93.     {
  94.         $availableConnections self::getConnections($configConnDealersync::class, []);
  95.         // echo count($availableConnections)." DealerCarSearch Files found\n";
  96.         if(count($availableConnections) > 0) {
  97.             return $availableConnections;
  98.         }
  99.         return null;
  100.     }
  101.     public static function getDownloadAdapter(CustomerConfig $configstring $type ''bool $raw false) : ?FilesystemAdapter
  102.     {
  103.         return self::getAdapter($config, ['in''bi'], $type$raw);
  104.     }
  105.     public static function getBeelinkAdapter(CustomerConfig $config) : mixed
  106.     {
  107.         $availableConnections self::getConnections($configConnBee2Link::class, []);
  108.         if(count($availableConnections) > 0) {
  109.             return $availableConnections;
  110.         }
  111.         return null;
  112.     }
  113.     public static function getOpenFlexAdapter(CustomerConfig $config) : mixed
  114.     {
  115.         $availableConnections self::getConnections($configConnOpenFlex::class, []);
  116.         if(count($availableConnections) > 0) {
  117.             return $availableConnections;
  118.         }
  119.         return null;
  120.     }
  121.     
  122.     public static function getCarBaseAdapter(CustomerConfig $config) : mixed
  123.     {
  124.         $availableConnections self::getConnections($configConnCarbase::class, []);
  125.         if(count($availableConnections) > 0) {
  126.             return $availableConnections;
  127.         }
  128.         return null;
  129.     }
  130.     public static function getWebhookAdapter(CustomerConfig $config) : mixed
  131.     {
  132.         $availableConnections self::getConnections($configConnWebhook::class, []);
  133.         if(count($availableConnections) > 0) {
  134.             return $availableConnections;
  135.         }
  136.         return null;
  137.     }
  138.     public static function getFastbackAdapter(CustomerConfig $config) : mixed
  139.     {
  140.         $availableConnections self::getConnections($configConnFastback::class, []);
  141.         if(count($availableConnections) > 0) {
  142.             echo count($availableConnections)." Fastback connections found\n";
  143.             return $availableConnections;
  144.         }
  145.         return null;
  146.     }
  147.     public static function getWaykeAdapter(CustomerConfig $config) : mixed
  148.     {
  149.         $availableConnections self::getConnections($configConnWayke::class, []);
  150.         if(count($availableConnections) > 0) {
  151.             return $availableConnections;
  152.         }
  153.         return null;
  154.     }
  155.     public static function getDATAdapter(CustomerConfig $config) : mixed
  156.     {
  157.         $availableConnections self::getConnections($configConnDAT::class, []);
  158.         if(count($availableConnections) > 0) {
  159.             echo count($availableConnections)." DAT connections found\n";
  160.             return $availableConnections;
  161.         }
  162.         return null;
  163.     }
  164.     public static function getPlanetVoAdapter(CustomerConfig $config) : mixed
  165.     {
  166.         $availableConnections self::getConnections($configConnPlanetVO::class, []);
  167.         if(count($availableConnections) > 0)
  168.         {
  169.             echo count($availableConnections)." PVO connections found\n";
  170.             return $availableConnections;
  171.         }
  172.         return null;
  173.     }
  174.     public static function getAutoTelexAdapter(CustomerConfig $config) : mixed
  175.     {
  176.         $availableConnections self::getConnections($configConnAutoTelex::class, []);
  177.         if(count($availableConnections) > 0)
  178.         {
  179.             echo count($availableConnections)." AutoTelex connections found\n";
  180.             return $availableConnections;
  181.         }
  182.         return null;
  183.     }
  184.     public static function getKeplerVOAdapter(CustomerConfig $config) : ?ConnKeplerVO
  185.     {
  186.         $availableConnections self::getConnections($configConnKeplerVO::class, []);
  187.         if(count($availableConnections) > 0)
  188.         {
  189.             return array_pop($availableConnections);
  190.         }
  191.         return null;
  192.     }
  193.     public static function getHomeNetAdapter(CustomerConfig $config) : mixed
  194.     {
  195.         $availableConnections self::getConnections($configConnHomeNet::class, []);
  196.         if(count($availableConnections) > 0)
  197.         {
  198.             return $availableConnections;
  199.         }
  200.         //echo "no HomeNet connections found\n";
  201.         return null;
  202.     }
  203.     
  204.     
  205.     public static function getMaxDigitalAdapter(CustomerConfig $config) : mixed
  206.     {
  207.         $availableConnections self::getConnections($configConnMaxDigital::class, []);
  208.         if(count($availableConnections) > 0)
  209.         {
  210.             return $availableConnections;
  211.         }
  212.         //echo "no HomeNet connections found\n";
  213.         return null;
  214.     }
  215.     private static function getAdapter(CustomerConfig $config, array $directionsstring $type ''bool $raw false) : mixed
  216.     {
  217.         $availableConnections self::getConnections($config$type$directions);
  218.         if(count($availableConnections) > 0) {
  219.             $probe array_pop($availableConnections);
  220.             if(!$raw && $probe::class === ConnFTPSettings::class)
  221.             {
  222.                 return self::getFtpAdapter($probe);
  223.             }
  224.             elseif ($raw && $probe::class === ConnFTPSettings::class)
  225.             {
  226.                 return $probe;
  227.             }
  228.         }
  229.         return null;
  230.     }
  231.     public static function getFtpAdapter(ConnFTPSettings $config) : ?FtpAdapter
  232.     {
  233.         return new FtpAdapter(
  234.             // Connection options
  235.             \League\Flysystem\Ftp\FtpConnectionOptions::fromArray([
  236.                 'host' => $config->getHostName(), // required
  237.                 'root' => $config->getBaseFolder(), // required
  238.                 'username' => $config->getUser(), // required
  239.                 'password' => $config->getPassword(), // required
  240.                 'port' => intval($config->getPort()),
  241.                 'ssl' => $config->getSsl(),
  242.                 'timeout' => 90,
  243.                 'utf8' => $config->getUseUtf(),
  244.                 'passive' => true,
  245.                 'transferMode' => FTP_BINARY,
  246.                 'systemType' => null// 'windows' or 'unix'
  247.                 'ignorePassiveAddress' => $config->getIgnorePassiveAddress(), // true or false
  248.                 'timestampsOnUnixListingsEnabled' => false// true or false
  249.                 'recurseManually' => true // true
  250.             ])
  251.         );
  252.     }
  253.     public static function getOpenSshKeyContent(): string
  254.     {
  255.         // Your Ed25519 key in OpenSSH format
  256.         return '
  257. -----BEGIN OPENSSH PRIVATE KEY-----
  258. b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
  259. QyNTUxOQAAACBsrD0ryU8Z6Kpxqp+oqq1l8IazPkIgBSu4dAPmDUjWywAAAJAsMenSLDHp
  260. 0gAAAAtzc2gtZWQyNTUxOQAAACBsrD0ryU8Z6Kpxqp+oqq1l8IazPkIgBSu4dAPmDUjWyw
  261. AAAEAOggxruNr75XIGp8lF0g7SMUt/WnIs8/foHcpD4aX47WysPSvJTxnoqnGqn6iqrWXw
  262. hrM+QiAFK7h0A+YNSNbLAAAACmNhcmNlbnRyYWwBAgM=
  263. -----END OPENSSH PRIVATE KEY-----
  264.         ';
  265.     }
  266.     public static function findLatestDealerCenterFile(Filesystem $filesystemstring $dealerId): ?string
  267.     {
  268.         try {
  269.             $files $filesystem->listContents('/'false);
  270.             $dealerCenterFiles = [];
  271.             
  272.             foreach ($files as $file) {
  273.                 $fileName basename($file->path());
  274.                 if ($file->isFile() && preg_match("/^DealerCenter_(\d{8})_{$dealerId}\.csv$/"$fileName$matches)) {
  275.                     $dealerCenterFiles[$matches[1]] = $file->path();
  276.                 }
  277.             }
  278.             
  279.             if (empty($dealerCenterFiles)) {
  280.                 echo "No DealerCenter files found for dealer ID: $dealerId\n";
  281.                 return null;
  282.             }
  283.             
  284.             // Get the latest file by date
  285.             ksort($dealerCenterFiles);
  286.             $latestDate array_key_last($dealerCenterFiles);
  287.             $latestFile $dealerCenterFiles[$latestDate];
  288.             
  289.             echo "Found latest DealerCenter file: $latestFile (date: $latestDate)\n";
  290.             return $latestFile;
  291.             
  292.         } catch (Exception $e) {
  293.             echo "Error finding DealerCenter files: " $e->getMessage() . "\n";
  294.             return null;
  295.         }
  296.     }
  297.     
  298.     public static function getSftpAdapter(array $config): ?SftpAdapter
  299.     {
  300.         
  301.         try {
  302.             //  to be handled such that it takes token / client type and rerturn respective key
  303.             // $inst = new self();
  304.             // $privateKey = self::getOpenSshKeyContent();
  305.             print_r($config['user']);
  306.             $connectionProvider = new SftpConnectionProvider(
  307.                 $config['host'],
  308.                 $config['user'],
  309.                 $config['password'] ?: null,
  310.                 $config['openssh_key'] ?: null,
  311.                 null ,
  312.                 intval($config['port'] ?: 22),
  313.                 false,   // use agent
  314.                 30,      // timeout
  315.                 10,      // max tries
  316.                 null,    // host fingerprint
  317.                 null     // connectivity checker
  318.             );
  319.             $adapter = new SftpAdapter(
  320.                 $connectionProvider,
  321.                  '/'
  322.             );
  323.             // ✅ Verify connectivity by trying a simple operation
  324.             $filesystem = new Filesystem($adapter);
  325.             $filesystem->listContents('.'false);
  326.             // echo" File system -> "+$filesystem;
  327.             return $adapter;
  328.         } catch (Throwable $e) {
  329.             // ❌ Handle connection/adapter errors gracefully
  330.             error_log("SFTP connection failed: " $e->getMessage());
  331.             echo "❌ Error: " $e->getMessage() . PHP_EOL;
  332.             print_r($e->getTrace());
  333.             return null;  // or throw new \RuntimeException("SFTP Error: " . $e->getMessage());
  334.         }
  335.     }
  336.     public static function getConnections(CustomerConfig $configstring $type '', array $directions) : mixed
  337.     {
  338.         $connections $config->getConnections();
  339.         if($connections->getCount() === 0) {
  340.             return [];
  341.         }
  342.         $connItems $connections->getItems();
  343.         $availableConnections array_filter($connItems, function($c) use ($type$directions) {
  344.             if($type === '' || $c::class === $type) {
  345.                 if(method_exists($c'getDirection')) {
  346.                     if(in_array($c->getDirection(), $directions)) {
  347.                         return true;
  348.                     }
  349.                 } else {
  350.                     return true;
  351.                 }
  352.             }
  353.             return false;
  354.         });
  355.         return $availableConnections;
  356.     }
  357.     public static function getDealerCenterAdapter($config): array
  358.     {
  359.         $connections $config->getConnections();
  360.         $dealerCenterConnections = [];
  361.         if ($connections) {
  362.             foreach ($connections->getItems() as $item) {
  363.                 if ($item instanceof ConnDealerCenter) {
  364.                     $dealerCenterConnections[] = $item;
  365.                 }
  366.             }
  367.         }
  368.         return $dealerCenterConnections;
  369.     }
  370. }
  371.