diff --git a/src/Builders/QueryBuilder.php b/src/Builders/ConnectionBuilder.php similarity index 92% rename from src/Builders/QueryBuilder.php rename to src/Builders/ConnectionBuilder.php index 0d48e27..8c0fde4 100644 --- a/src/Builders/QueryBuilder.php +++ b/src/Builders/ConnectionBuilder.php @@ -6,7 +6,7 @@ use Lightmedia\Googleprint\Cache\GooglePrintCache; use Lightmedia\Googleprint\Exceptions\GooglePrintException; -class QueryBuilder { +class ConnectionBuilder { protected $accessToken; protected $values; @@ -75,6 +75,15 @@ public function submit(array $array) { return $result; } + public function printer(array $array) { + + $this->values = $array; + + $result = $this->request(self::BASE_URL . '/printer'); + + return $result; + } + protected function removeEmptyValues() { foreach($this->values as $key => $value) { @@ -115,7 +124,7 @@ protected function request($url) { if(true !== $response['success']){ - throw new GooglePrintException('Error in Google API request: ' . $response['message']); + throw new GooglePrintException('Error in Google API request: ' . $response['message'] . ' in ' . $url); } return $response; diff --git a/src/Builders/PrinterBuilder.php b/src/Builders/PrinterBuilder.php index 50f1d55..2784835 100644 --- a/src/Builders/PrinterBuilder.php +++ b/src/Builders/PrinterBuilder.php @@ -119,7 +119,7 @@ public function all() { public function get() { - $search = new QueryBuilder($this->accessToken); + $search = new ConnectionBuilder($this->accessToken); $response = $search->search($this->values); @@ -134,15 +134,15 @@ protected function arrayToHash(array $array) { protected function buildObjects(Array $array) { - $return = []; + $return = [ ]; - $hide = config('print.printers.hide') ?: []; + $hide = config('print.printers.hide') ?: [ ]; foreach($array as $item) { - if(false === in_array($item['id'], $hide)){ + if(false === in_array($item['id'], $hide)) { - $object = new Printer($this->accessToken, $item['id']); + $object = new Printer($item['id']); $return[] = $object->assign($item); } @@ -150,4 +150,24 @@ protected function buildObjects(Array $array) { return collect($return); } + + public function find($id) { + + $params = [ + 'printerid' => $id, + 'extra_fields' => 'connectionStatus,semanticState,uiState', + ]; + + $search = new ConnectionBuilder; + $response = $search->printer($params); + + if(count($response['printers']) !== 1){ + throw new GooglePrintException('Unable to find printer'); + } + + $object = new Printer; + $object->assign($response['printers'][0]); + + return $object; + } } diff --git a/src/Cache/GooglePrintCache.php b/src/Cache/GooglePrintCache.php index 4d84afe..c0951c7 100644 --- a/src/Cache/GooglePrintCache.php +++ b/src/Cache/GooglePrintCache.php @@ -2,7 +2,6 @@ namespace Lightmedia\Googleprint\Cache; use Cache; -use Lightmedia\Googleprint\Builders\QueryBuilder; class GooglePrintCache extends Cache { @@ -20,8 +19,6 @@ public static function __callStatic($name, $arguments) { protected static function cache() { - $request = new QueryBuilder; - return Cache::store(config('print.cache.driver')); } } \ No newline at end of file diff --git a/src/Facades/GooglePrint.php b/src/Facades/GooglePrint.php new file mode 100644 index 0000000..b3bf187 --- /dev/null +++ b/src/Facades/GooglePrint.php @@ -0,0 +1,14 @@ +find(config('print.printers.default')); + } + + public static function newPrintJob() { + + return new PrintJob; + } } diff --git a/src/GoogleprintServiceProvider.php b/src/GoogleprintServiceProvider.php index 911e917..55a82fb 100644 --- a/src/GoogleprintServiceProvider.php +++ b/src/GoogleprintServiceProvider.php @@ -23,6 +23,6 @@ public function boot() */ public function register() { - // + $this->app->bind('Googleprint', 'LightMedia\Googleprint\Googleprint' ); } } diff --git a/src/Models/PrintJob.php b/src/Models/PrintJob.php index 0f7c773..89bc76a 100644 --- a/src/Models/PrintJob.php +++ b/src/Models/PrintJob.php @@ -1,9 +1,9 @@ $this->getContentType(), ]; - $request = new QueryBuilder(); + $request = new ConnectionBuilder; $response = $request->submit($params); $this->assign($response['job']); @@ -104,6 +104,8 @@ protected function getTicket() { public function printer(Printer &$printer) { $this->printer = &$printer; + + return $this; } public function view($view, $variables = [ ]) { diff --git a/src/Models/Printer.php b/src/Models/Printer.php index d1f4853..e5eeb4d 100644 --- a/src/Models/Printer.php +++ b/src/Models/Printer.php @@ -2,6 +2,7 @@ namespace Lightmedia\Googleprint\Models; use Lightmedia\Googleprint\Exceptions\GooglePrintException; +use Lightmedia\Googleprint\Models\PrintJob; class Printer extends BaseObject{ @@ -22,12 +23,11 @@ protected function getFullObject() { } - public function submit() { + public function submit(PrintJob $job) { $this->canPrint(); - - + return $job->printer($this)->save(); } protected function getStatusAttribute() { @@ -74,13 +74,7 @@ protected function getNameAttribute() { return null; } } - - protected function getIdAttribute() { - - return strtolower($this->id); - } - - + protected function canPrint() { if(true === $this->values['isTosAccepted']) {