3 * ProductRegistrationService
5 * LICENSE: This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
17 * @author Matthew Vita <matthewvita48@gmail.com>
18 * @author Victor Kofia <victor.kofia@gmail.com>
19 * @link http://www.open-emr.org
24 require_once($GLOBALS['fileroot'] . "/interface/main/exceptions/invalid_email_exception.php");
25 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/generic_product_registration_exception.php");
26 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/duplicate_registration_exception.php");
28 class ProductRegistrationService
{
30 * Logger used primarily for logging events that are of interest to
36 * The product registration repository to be used for db CRUD operations.
41 * Default constructor.
43 public function __construct()
45 $this->logger
= new \common\logging\
Logger("\services\ProductRegistrationService");
46 $database = \common\database\Connector
::Instance();
47 $entityManager = $database->entityManager
;
48 $this->repository
= $entityManager->getRepository('\entities\ProductRegistration');
51 public function getProductStatus()
53 $this->logger
->debug('Getting current product registration status');
54 $row = $this->repository
->findFirst();
56 // Unboxing these here to avoid PHP 5.4 "Can't use method
57 // return value in write context" error.
62 $id = $row->getRegistrationId();
63 $optOut = $row->getOptOut();
67 $row = new \entities\
ProductRegistration();
68 $row->setStatusAsString('UNREGISTERED');
69 } else if ($id !== 'null') {
70 $row->setStatusAsString('REGISTERED');
71 } else if (!empty($optOut) && $optOut == true) {
72 $row->setStatusAsString('OPT_OUT');
78 public function registerProduct($email)
80 if (!$email ||
$email == 'false') {
81 $this->optOutStrategy();
84 return $this->optInStrategy($email);
88 private function optInStrategy($email)
90 $this->logger
->debug('Attempting to register product with email ' . $email);
91 $curl = curl_init('https://reg.open-emr.org/api/registration');
92 curl_setopt($curl, CURLOPT_POST
, true);
93 curl_setopt($curl, CURLOPT_POSTFIELDS
, http_build_query(array('email' => $email)));
94 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
95 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, false);
96 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST
, 2);
97 $responseBodyRaw = curl_exec($curl);
98 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
101 $this->logger
->debug('Raw response from remote server: ' . $responseBodyRaw);
102 switch ($responseCode) {
104 $responseBodyParsed = json_decode($responseBodyRaw);
106 $entry = new \entities\
ProductRegistration();
107 $entry->setRegistrationId($responseBodyParsed->productId
);
108 $entry->setEmail($email);
109 $entry->setOptOut(false);
111 $newId = $this->repository
->save($entry);
112 $this->logger
->debug('Successfully registered product ' . $newId);
117 throw new \
InvalidEmailException($email . ' ' . xl("is not a valid email address"));
120 throw new \
DuplicateRegistrationException(xl("Already registered"));
123 throw new \
GenericProductRegistrationException(xl("Server error: try again later"));
127 // void... don't bother checking for success/failure.
128 private function optOutStrategy()
130 $this->logger
->debug('Attempting to opt out of product registration');
131 $entry = new \entities\
ProductRegistration();
132 $entry->setRegistrationId('null');
133 $entry->setEmail(null);
134 $entry->setOptOut(true);
136 $this->repository
->save($entry);