Skip to content

Commit

Permalink
refs singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
desarrolla2 committed Apr 28, 2012
1 parent 7ea2206 commit 3048db8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 29 deletions.
15 changes: 6 additions & 9 deletions yii-app/protected/models/Book.php
Expand Up @@ -16,18 +16,15 @@
class Book
{

public $google_link = null;
public $link = null;
public $title = null;
public $description = null;
public $author = null;
public $description = null;
public $category = null;


public function save(){
$parse = new parseRestClient(array(
'appid' => 'YOUR APPLICATION ID',
'restkey' => 'YOUR REST KEY ID'
));

public function save()
{
// ..
}

}
77 changes: 77 additions & 0 deletions yii-app/protected/models/BooksParseService.php
@@ -0,0 +1,77 @@
<?php

require_once(dirname(__FILE__) . '/BooksCollection.php');
require_once(dirname(__FILE__) . '/Book.php');
require_once(dirname(__FILE__) . '/../vendors/parse.php');

/*
* This file is part of the Instabook package.
*
* Description of BooksService
*
* @author Daniel Gonzalez <daniel.gonzalez@freelancemadrid.es>
* @file BooksService.php
* @date 28 - Apr - 2012
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class BooksParseService
{

private static $instances = array();
private $parse = null;

protected function initialize()
{
$this->parse = new parseRestClient(array(
'appid' => 'u5H31glPx0jAhIpud6NhnEMzCQhEEIPgH5yk10fm',
'restkey' => 'sBPGrYzBu2HKwZ6bCsg2HAGwBlj5rRDpjzXuuAwo'
));
}

final public static function getInstance()
{

$class = get_called_class();
if (!isset(self::$instances[$class]))
{
self::$instances[$class] = new $class();
}

return self::$instances[$class];
}

public function __construct()
{
if (isset(self::$instances[get_called_class()]))
{
throw new Exception(" A " . get_called_class() . " instance already exist");
}
$this->initialize();
}

public function search($query)
{

}

public function save(Book $book)
{
$params = array(
'className' => 'Books',
'object' => array(
'hash' => md5($book->link),
'link' => $book->link,
'title' => $book->title,
//'author' => $book->author,
'description' => $book->description,
)
);
$response = $this->parse->create($params);

die(var_dump($response));
}

}
18 changes: 0 additions & 18 deletions yii-app/protected/models/BooksService.php
Expand Up @@ -29,11 +29,6 @@ protected function initialize()
{
$this->goole_rest_api_search = 'https://www.googleapis.com/books/v1/volumes';
$this->collection = new BooksCollection();

$this->parse = new parseRestClient(array(
'appid' => 'u5H31glPx0jAhIpud6NhnEMzCQhEEIPgH5yk10fm',
'restkey' => 'sBPGrYzBu2HKwZ6bCsg2HAGwBlj5rRDpjzXuuAwo'
));
}

final public static function getInstance()
Expand Down Expand Up @@ -93,17 +88,4 @@ public function search($query)
return $this->collection;
}

public function save(Book $book)
{
$params = array(
'className' => 'Books',
'object' => array(
'title' => $book->title,
'author' => $book->author,
''
)
);
$request = $parse->create($params);
}

}
7 changes: 5 additions & 2 deletions yii-app/protected/tests/books/service.php
@@ -1,10 +1,13 @@
<?php

require_once(dirname(__FILE__) . '/../../models/BooksService.php');
require_once(dirname(__FILE__) . '/../../models/BooksParseService.php');

$query = "el señor de los anillos";

$service = BooksService::getInstance();
$books = $service->search($query);
$books = BooksService::getInstance()->search($query);
$books = $books->toArray();


$response = BooksParseService::getInstance()->save($books[0]);
die(var_dump($books));

0 comments on commit 3048db8

Please sign in to comment.