3 * ProductRegistrationService
6 * @link http://www.open-emr.org
7 * @author Matthew Vita <matthewvita48@gmail.com>
8 * @author Victor Kofia <victor.kofia@gmail.com>
9 * @copyright Copyright (c) 2017 Matthew Vita <matthewvita48@gmail.com>
10 * @copyright Copyright (c) 2017 Victor Kofia <victor.kofia@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 namespace OpenEMR\Services
;
17 use OpenEMR\Common\Database\Connector
;
18 use OpenEMR\Common\Logging\Logger
;
19 use OpenEMR\Entities\ProductRegistration
;
21 require_once($GLOBALS['fileroot'] . "/interface/main/exceptions/invalid_email_exception.php");
22 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/generic_product_registration_exception.php");
23 require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/duplicate_registration_exception.php");
25 class ProductRegistrationService
28 * Logger used primarily for logging events that are of interest to
34 * The product registration repository to be used for db CRUD operations.
39 * Default constructor.
41 public function __construct()
43 $this->logger
= new Logger("\OpenEMR\Services\ProductRegistrationService");
44 $database = Connector
::Instance();
45 $entityManager = $database->entityManager
;
46 $this->repository
= $entityManager->getRepository('\OpenEMR\Entities\ProductRegistration');
49 public function getProductStatus()
51 $this->logger
->debug('Getting current product registration status');
52 $row = $this->repository
->findFirst();
54 // Unboxing these here to avoid PHP 5.4 "Can't use method
55 // return value in write context" error.
60 $id = $row->getRegistrationId();
61 $optOut = $row->getOptOut();
65 $row = new ProductRegistration();
66 $row->setStatusAsString('UNREGISTERED');
67 } else if ($id !== 'null') {
68 $row->setStatusAsString('REGISTERED');
69 } else if (!empty($optOut) && $optOut == true) {
70 $row->setStatusAsString('OPT_OUT');
76 public function registerProduct($email)
78 if (!$email ||
$email == 'false') {
79 $this->optOutStrategy();
82 return $this->optInStrategy($email);
86 private function optInStrategy($email)
88 $this->logger
->debug('Attempting to register product with email ' . $email);
89 $curl = curl_init('https://reg.open-emr.org/api/registration');
90 curl_setopt($curl, CURLOPT_POST
, true);
91 curl_setopt($curl, CURLOPT_POSTFIELDS
, http_build_query(array('email' => $email)));
92 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
93 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER
, false);
94 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST
, 2);
95 $responseBodyRaw = curl_exec($curl);
96 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
99 $this->logger
->debug('Raw response from remote server: ' . $responseBodyRaw);
100 switch ($responseCode) {
102 $responseBodyParsed = json_decode($responseBodyRaw);
104 $entry = new ProductRegistration();
105 $entry->setRegistrationId($responseBodyParsed->productId
);
106 $entry->setEmail($email);
107 $entry->setOptOut(false);
109 $newId = $this->repository
->save($entry);
110 $this->logger
->debug('Successfully registered product ' . $newId);
115 throw new \
InvalidEmailException($email . ' ' . xl("is not a valid email address"));
118 throw new \
DuplicateRegistrationException(xl("Already registered"));
121 throw new \
GenericProductRegistrationException(xl("Server error: try again later"));
125 // void... don't bother checking for success/failure.
126 private function optOutStrategy()
128 $this->logger
->debug('Attempting to opt out of product registration');
129 $entry = new ProductRegistration();
130 $entry->setRegistrationId('null');
131 $entry->setEmail(null);
132 $entry->setOptOut(true);
134 $this->repository
->save($entry);