johnhamelink
1/26/2013 - 8:28 PM

sourcesmanager.php

<?php

namespace pdimporter;
use \Exception;

class sourcesManager
{

    private $_classesDir = null;
    private $_sources    = [];

    public function __construct()
    {
        // Get a list of all the classes we intend to load
        $this->_classesDir = glob(__DIR__ . "/sources/*.inc.php");
        foreach ($this->_classesDir as $class) {
            // Including the class doesn't initialise it, so
            // it's not a big overhead.
            include_once $class;
        }
    }

    public function getClassesList()
    {
        // Get a list of all declared classes.
        $classes = get_declared_classes();

        foreach ($classes as $index => $class) {
            // Remove all the classes which are not in the sources
            // namespace
            if (!strstr($class, "pdimporter\sources\\")) {
                unset($classes[$index]);
            }
        }

        // Reset the element count
        $classes = array_values($classes);

        return $classes;
    }

    public function getOutdatedList()
    {
        $result = [];
        // For each source class
        foreach($this->getClassesList() as $class) {
            // Get the model from the class
            $model =  $class::$sourceModel;
            // Test that our data is old enough for us to rescan
            $lastEntry = $model::where('name', '=', $class::$name)
                ->where('lastSyncAttempt', '>', new \DateTime($class::$staleThreshold))
                ->take(1)
                ->get();

            // If it is, then add it to the list of classes to sync with
            if (count($lastEntry) == 0) {
                $result[] = $class;
            }
        }

        return $result;
    }

    public function sync($class) {
        // Get the model from the class
        $model = $class::$model;
        // Define a new instance of the class, defining a callback
        // with logic on saving the output array
        echo "\t - [{$class::$name}] Saving Output".PHP_EOL;
        $sync = new $class(function($stdArr) use($model, $class) {
            // For each article, attempt to add a row to the DB - the
            // article title must be unique or it won't save
            foreach ($stdArr as $stdOut) {
                $m = new $model($stdOut);
                try {
                    $m->save();
                // Catch a failed db insertion
                } catch (\Exception $err) {
                    \Log::warn("Error, unable to add row from {$class::$name} entitled {$m->title}.\n");
                    // Attempt to save the next row.
                    continue;
               }
            }
            echo "\t - [{$class::$name}] Done".PHP_EOL;
            return true;
        });

        return $sync;
    }

    public function syncArray(Array $sourcesList)
    {
        echo "Attempting sync of following sources:" . PHP_EOL;
        foreach ($sourcesList as $listItem) {
            echo "\t - {$listItem::$name}" . PHP_EOL;
        }
        echo PHP_EOL . PHP_EOL;

        for($i = 0; $i < count($sourcesList); $i++) {
            echo "\t - [{$sourcesList[$i]::$name}] Sync Started" . PHP_EOL;
            \Log::info("[{$sourcesList[$i]::$name}] Sync Started");

            if ($this->sync($sourcesList[$i])) {

                echo "\t - [{$sourcesList[$i]::$name}] Sync Completed" . PHP_EOL . PHP_EOL;
                \Log::info("[{$sourcesList[$i]::$name}] Sync Completed");

            } else {

                echo "\t - [{$sourcesList[$i]::$name}] Sync Failed" . PHP_EOL . PHP_EOL;
                \Log::err("[{$sourcesList[$i]::$name}] Sync Failed");

            }

        }

    }

}